Unix Shell - The if...fi statement
The if...fi statement is the fundamental control statement that allows Shell to make decisions and execute statements conditionally.
if [ expression ] then Statement(s) to be executed if expression is true fi
The Shell expression is evaluated in the above syntax. If the resulting value is true, given statement(s) are executed. If the expression is false then no statement would be executed. Most of the times, comparison operators are used for making decisions.
It is recommended to be careful with the spaces between braces and expression. No space produces a syntax error.
If expression is a shell command, then it will be assumed true if it returns 0 after execution. If it is a Boolean expression, then it would be true if it returns true.
#!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" fi if [ $a != $b ] then echo "a is not equal to b" fi
The above script will generate the following result −
a is not equal to b