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

20 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. Mitch says:

    Thank you. Fixed error when I used wildcard between braces

  13. makomi says:

    Thank you.

  14. makomi says:

    Thanks!

  15. 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.

  16. Bill says:

    Saved my sorry a$$!!! Thanks!

  17. bob says:

    i will add to the list of thank yous!

  18. Rob says:

    Big help! Thank you!

  19. 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

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

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