Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command inside if statement of bash script [duplicate]

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?

like image 800
myahya Avatar asked Mar 11 '11 17:03

myahya


People also ask

What is in if condition in shell script?

The keyword if is followed by a condition. This condition is evaluated to decide which statement will be executed by the processor. If the condition evaluates to TRUE, the processor will execute the statement(s) followed by the keyword then. In the syntax, it is mentioned as statement1.

Can you have multiple if statements in bash?

Bash else-if statement is used for multiple conditions. It is just like an addition to Bash if-else statement. In Bash elif, there can be several elif blocks with a boolean expression for each one of them. In the case of the first 'if statement', if a condition goes false, then the second 'if condition' is checked.

How do you use multiple IF statements in shell script?

To use multiple conditions in one if-else block, then elif keyword is used in shell. If expression1 is true then it executes statement 1 and 2, and this process continues. If none of the condition is true then it processes else part.


1 Answers

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 
like image 186
Alex Spurling Avatar answered Sep 29 '22 12:09

Alex Spurling