Condition Statements in Linux

Every UNIX command returns a value on exit which the shell can interrogate. This value is held in the read only shell variable $?

Every UNIX command returns a value on exit which the shell can interrogate. This value is held in the read only shell variable $?. A value of 0 (zero) signifies failure.

If Statement

This statement uses the exit status of the given command and makes a decision based on a condition.
Shell supports following forms of if statement.

  • If-fi statement :- In This statement, if the condition is true, then command are performed. If the condition is false, nothing is done.

This syntax is:
if condition ; then
commands
fi

  • If-else-fi statement :- In this statement, if the condition is true, the first set of command is performed. The condition is false, the second set of command is performed.

The syntax is:
if condition ; then
commands
else
commands
fi

  • If-elif-fi statement :- In this statement, if the condition is true, then the first set of commands is performed. If the condition id false, and if the second condition is true, then the second set of command is performed.

The syntax is:
if condition ; then
commands
elif condition ; then
commands
fi