So if you ever decide to get into shell scripting, it’s a wonderful world. Until you run into snags that you once thought you had the hang of. Let’s take shell conditionals, for example.
So i’m trying to test the output of grep and do something if it returns a result. After setting it up the usual way:
if [ -n `ls | grep something' ]; then
echo "Something Exists";
fi
But by doing it that way, I kept getting the error:
./scnotify.sh: line 3: [: 2841.c: binary operator expected
which for the life of me I couldn’t figure out. Eventually I figured it out though…and all that the error was, was instead of a single ‘[‘ and ‘]’ wrapping the comparison, it requires a double…i.e.
if [[ -n `ls | grep something` ]]; then
echo "Success";
fi
Hope this saves someone some headache in the future.
beacuse execute “ls | grep something” maybe more than one parameter generate,
It truly works, I had a problem with this line
if [ grep -i mars $1 ] ; then
so I changed it for
if [[ -n `grep -i mars $1` ]] ; then
do you know why it works?
or you do it right.
if [ -n “`ls | grep something'” ]; then
echo “Something Exists”;
fi
As a matter of fact, this did save me from a headache. Thanks!
thanks! that really helped me =) no headache here, thanks to you
You just saved my sanity. Thanks for this post. VERY helpful!
big THX 4 saving me be4 headache ;-)
it helped! but why?!? doesn’t matter ;)
GreetX from Switzerland
VoiceX
Totally saved me a headache. Thanks!
Great, saved me a headache. After researching, the bash man page says “Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons.
Doesn’t seem to work that way though.
[ -d ${TMPDIR} ] || mkdir ${TMPDIR} # <– Works
[[ -e ${FILE} ]] || echo "$FILE not found" # <– Doesn't work without double brackets
Heachache resolved…thx for the tip!
Thanks for the advice, little tricky bug to figure out.
using backticks on commands is depricated. use parenths. And no need for a semi-colon after the echo command. Newline signifies start of a new command.
if [[ -n $( ls | grep something ) ]]; then
echo “Success”
fi
Thanks for the help
criticalfs=`df -ah|grep 100%|wc -l`
if [ $criticalfs -h 0 ]
then
echo “” >>$log
echo “There are $criticalfs Filesystems in Critical status.” >>$log
df -ah|grep 100% >>$log
status=”CRITICAL:”
message=”Filesystem is Full”
utl_mail
exit 0
fi
Can anyone tell me what’s wrong with above code? I am using SUSE linux. I am getting below error always! thanks!!
./df_interval.sh: line 30: [: -ah: binary operator expected
Thank you. Fixed error when I used wildcard between braces
Thank you.
Thanks!
Thank you very much. This is a bug I had in a cygwin bashrc script and as I am not used to bash syntax this tricky problem eluded me till now. I have found the if condition that was causing me the problem.
Saved my sorry a$$!!! Thanks!
i will add to the list of thank yous!
Big help! Thank you!
Thanks! You just made my day regarding this little puppy:
if [[ “$EXEC_VER” =~ “$LATEST_VERSION” ]] && [[ “$EXEC_OSVER” =~ “$LATEST_OSVER” ]]; then
echo “You are using latest.”
exit 0
fi
Saved me a headache too. Thanks!
In your example, the [ ] form of test is using word splitting. Chances are, your `ls | grep` statement returns more than one token/word. Hence, the message “Binary Operator Expected” ([ ] expects to see operators between the resulting tokens). The [[ ]] form of test does not perform word splitting (tokenizing) and will see your `ls | grep` results as one string instead of many words. That’s the gist of it, anyway. :-)
Thanks i researched this for days and finally came across your post
God blass you! Thanks!
thanks it helped me. God bless you.
You just saved me hours of debugging. Thank you!
Thank you SO much for helping me to get around this frustratingly obscure error!