Command inside if statement of bash script

myahya picture myahya · Mar 11, 2011 · Viewed 76.6k times · Source

I have the following line as part of a much bigger bash script:

if [ `packages/TinySVM-0.09/bin/svm_learn 2>&1| grep TinySVM | wc -l | cut -c0-7 | sed 's/^  *//g'` -eq 1 ] 

upon running the script, I get:

./install.sh: line 219: [: -eq: unary operator expected

Where line 219 is the line above. Any suggestions for a fix?

Answer

Alex Spurling picture Alex Spurling · Nov 12, 2012

You can run your command without any additional syntax. For example, the following checks the exit code of grep to determine whether the regular expression matches or not:

if ! grep -q "$word" /usr/share/dict/words
then
    echo "Word $word is not valid word!"
fi