I am trying to figure out the syntax for having a while loop and an if statement that checks for more than one condition, in a single-line shell script.
Executing something like this...
i=2; while [ $i -le 10 ]; do if [ $i -ne 3 -a $i -ne 5 ] echo $i " not equal to 3 or 5"; else echo $i; i=`expr $i + 1`; done
...I get the error
bash: syntax error near unexpected token `else'
On another hand if I remove the semicolon from between ...3 or 5" and else echo..., and try something like this...
i=2; while [ $i -le 10 ]; do if [ $i -ne 3 -a $i -ne 5 ] echo $i " not equal to 3 or 5" else echo $i; i=`expr $i + 1`; done
...then I get the error:
syntax error near unexpected token `done'
This is on an Ubuntu 14.04, in case it matters.
Am I perhaps missing some kind of a parenthesis somewhere, or is it something else?
';'wherever you would normally have a newline if you had written the script as a normally-formatted multi-line script. As noted below, you have missingthenandfi's as well.