Bash shell 'if' statement comparing outputs from different commands

13,435

Solution 1

At least in other shells, you need to be a lot more careful with spaces; the square bracket is a command name and needs to be separated from previous and following words. You also need (again in classic shells for certain) to embed the variables in double quotes:

app1=$(someapp -flag | grep usefulstuff | cut -c 5-10)
if [ "$app1" = "$(someapptwo -flag | grep usefulstuff | cut -c 20-25)" ]
then mkdir IPFolder-1
elif ...blah blah
then : do this instead...
fi

You could do it all in one line (well, two because I avoid the horizontal scrollbar whenever possible):

if [ "$(someapp    -flag | grep usefulstuff | cut -c  5-10)" = \
     "$(someapptwo -flag | grep usefulstuff | cut -c 20-25)" ]
then mkdir IPFolder-1
elif ...blah blah
then : do this instead...
fi

Or you could do it with two separate command captures:

app1=$(someapp    -flag | grep usefulstuff | cut -c  5-10)
app2=$(someapptwo -flag | grep usefulstuff | cut -c 20-25)
if [ "$app1" = "$app2" ]
then mkdir IPFolder-1
elif ...blah blah
then : do this instead...
fi

Update: Some extra quotes added. It would be possible to quote the assignments too:

app1="$(someapp -flag | grep usefulstuff | cut -c  5-10)"

No harm would be done; it isn't strictly necessary with bash (but it may well have been necessary with archaic Bourne shell).

Solution 2

You need to refer to the value of your expression by prepending a $:

...
if [ "$app1" = "$(someapptwo -flag | grep usefulstuff | cut -c 20-25)" ]; then
...
Share:
13,435
bikerben
Author by

bikerben

I’m Parker Software’s ThinkAutomation Technical Sales Consultant. With a technical background spanning over 20 years in various areas of the IT industry.

Updated on June 05, 2022

Comments

  • bikerben
    bikerben almost 2 years

    Using an adapted example given to me by Sam Ruby which I have tweaked so I can show what I'm trying to achieve.

    app1=$(someapp -flag | grep usefulstuff | cut -c 5-10)
    if [$app1 = (someapptwo -flag | grep usefulstuff | cut -c 20-25)]; then
    mkdir IPFolder-1
    elif ...blah blah
    fi 
    

    Can I use grep as show above or am I barking up the wrong tree? or should it look a little some thing like this:

    app1=$(someapp -flag | grep usefulstuff | cut -c 5-10)
    app2=$(someapptwo -flag | grep usefulstuff | cut -c 20-25)
    if [$app1 = $app2]; then
    mkdir IPFolder-1
    elif ...blah blah
    fi 
    
  • Gordon Davisson
    Gordon Davisson over 12 years
    You should also put double-quotes around the $(somecommand) bits, at least in the if statements (it's not necessary when assigning them to a variable, but I tend to do it anyway for consistency).
  • Jonathan Leffler
    Jonathan Leffler over 12 years
    I tend to quote them; I got castigated because it isn't always necessary, and I'm trying to lighten up. Maybe I shouldn't bother. I'd love to have access to a real Bourne shell to test more of what is the new bash behaviour vs what is legacy behaviour. I'll add the double quotes - thanks.
  • Jonathan Leffler
    Jonathan Leffler over 12 years
    Classic shells would include System V Bourne shell (and System III or 7th Edition versions), for example, and possibly the Korn shell too. Bash would be a modern shell.