Bash/Shell Programming – Binary Operator Expected

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.

About these ads

29 thoughts on “Bash/Shell Programming – Binary Operator Expected

  1. kevin06405 says:

    beacuse execute “ls | grep something” maybe more than one parameter generate,

  2. mikoto says:

    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?

  3. looser says:

    or you do it right.
    if [ -n “`ls | grep something'” ]; then
    echo “Something Exists”;
    fi

  4. As a matter of fact, this did save me from a headache. Thanks!

  5. someone says:

    thanks! that really helped me =) no headache here, thanks to you

  6. Gary says:

    You just saved my sanity. Thanks for this post. VERY helpful!

  7. VoiceX says:

    big THX 4 saving me be4 headache ;-)

    it helped! but why?!? doesn’t matter ;)

    GreetX from Switzerland
    VoiceX

  8. Ariel Alonso says:

    Totally saved me a headache. Thanks!

  9. Spivee says:

    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

  10. Rick says:

    Heachache resolved…thx for the tip!

  11. Corne says:

    Thanks for the advice, little tricky bug to figure out.

  12. mechaflash says:

    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

  13. shameer says:

    Thanks for the help

  14. Ratna Kumar says:

    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

  15. Mitch says:

    Thank you. Fixed error when I used wildcard between braces

  16. makomi says:

    Thank you.

  17. makomi says:

    Thanks!

  18. Mitul Amin says:

    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.

  19. Bill says:

    Saved my sorry a$$!!! Thanks!

  20. bob says:

    i will add to the list of thank yous!

  21. Rob says:

    Big help! Thank you!

  22. Mutual Friend says:

    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

  23. Jack Bauer says:

    Saved me a headache too. Thanks!

  24. 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. :-)

  25. smthing2do says:

    Thanks i researched this for days and finally came across your post

  26. Renato Cabelino says:

    God blass you! Thanks!

  27. Anon says:

    thanks it helped me. God bless you.

  28. You just saved me hours of debugging. Thank you!

  29. Lise says:

    Thank you SO much for helping me to get around this frustratingly obscure error!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s