An if statement evaluates a logical expression. If the expression
is true, the code inside the if is executed.
Shell uses test to evaluate expressions. You can look this up
in the man page for sh. Some commonly used expressions are listed in the
link for Logical Expressions.
I recommend using the square brackets (shown below).
Do NOT use parenthesis - this does not work efficiently in all versions of Linux, and should be avoided.
The basic format for an if in shell is:
if [ expression ]
then
command
command
fi
In shell, you may put as many statements inside the if as you would like.
The then and the fi are required, even if there is only
one statement to execute. (And yes, fi is if spelled backwards.)
Be careful! The square brackets are tricky - you must have the
whitespace around them.
Here is the format for an if-else:
if [ expression ]
then
command
else
command
fi
Here is the format for an if-else-if (elif), which can also have
an else at the end:
if [ expression ]
then
command
elif [ expression ]
then
command
fi