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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With