bash memo

I always forget the syntax of if…then..else…fi clauses

if test 1 -gt 0 then

echo yes

fi

will produce the error:-bash: syntax error near unexpected token `fi’

if test 1 -gt 0 then echo yes

fi

will produce the same error.

The correct notation is:

if test 1 -gt 0

then echo yes

fi

or,

if test 1 -gt 0

then

echo yes

fi

In fact every line (without ;) will be considered as a sub clause in bash. So if I write “if test 1 -gt 0 then” in one line, that would be a single sub-clause which is not correct in syntax because “if” sub-clause does not allow the occurrence of “then”. “then …” is another sub-clause.  But the following notations are also correct.

if

test 1 -gt 0

then

echo yes

fi

Bash will concatenate the following line to the “if” line to form a sub-clause, if nothing follows the “if” in the “if” line. The same goes with the “then” subclause.

If you write a wrong syntax, you can use the up arrow to call the last command to see how bash interprets your last command. Bash will show the last command separating each sub-clause with a ;

C programmers often make the following mistake:

if test 1 -gt 0

echo “1>0″

fi

Here, the “THEN” is omitted.

if clause tests the exit code of the command. If the command exits with 0, the then clause is executed.  If you want to execute the then clause when the command exits with a non-zero value, you can add a ! before the command, i.e.,

if ! command;then;fi

Note there is a space between the ! and the command.

The test command evaluates the expression(not  a command)  passed as its parameters and return 0 if the expression is evaluated to true.

test -n “$variable” returns 0 if $variable is not empty.

test -z “$variable” returns 0 if $variable is empty.

test 1 -gt 0 -a 2 -gt 1 returns 0;

test 1 -gt 0 -0 1 -gt 2 returns 0

The above description like those in classical unix books, will give you an illusion that only one condition(a command or test) can be used for the if statement. This is not correct. You can put multiple commands after if using && or ||, which acts like the logical operator “AND”,”OR”.  For example,

if grep aa in file && grep bb in file

then echo “both aa and bb occur in file”

fi

if grep aa in file || grep bb in file

then echo “aa or bb occurs in file”

fi

For &&, the first command is executed and if exit 0, the second command is executed, and IF tests the exit code of the second command to decide whether to execute the THEN sub-clause. If the first command exits non-zero, the second command will not be execute and IF tests the exit value of the first command(non-zero)  so it will not execute the THEN sub-clause.

For ||, the first command is executed and if exits with 0, the second command will not be executed, and IF tests the exit code of the first command(0) and decides to execute the THEN sub-clause. If the first command exits with non-zero, the second command will be executed and IF decides whether to execute the THEN sub-clause according to the exit code of the second command.

 

Posted in tips of hosting