Next: The case Statement
Up: Looping and Making Choices
Previous: The while And until
The simplest form on an if statement is this:
if control-command
then
commands
fi
- First the control-command is executed. If it is successfull (exit
status of 0), then the commands between the then and the fi are
performed. The words if, then and fi MUST come after a
command separator.
- While the normal if statement could be replaced by the conditional
operator notation there is the possibility to add an else option.
The general form of the if
else construction is this:
if control-command
then
commands
else
commands
fi
- First the control-command is executed. If it is successfull (exit
status of 0), then the commands between the then and the else are
performed. If it fails, the commands between the else and the fi
are executed.
- There is one further extension of the if statement that allows to
provide several alternatives (multiple elses) using the keyword
elif (short for else if).
The general form of the if then
elif then
else construction
looks like this:
if control-command 1
then
commands
elif control-command 2
then
commands
else
commands
fi
- If the first control-command (control-command1) is successful, then
the commands after the first then up to the elif are executed.
- Otherwise, control-command2 after the elif is attempted; if
it is successful, then the commands after the following then up to the
next elif or else are executed.
- The process continues until the closing fi is reached.
Roger Hampel
Mon Feb 2 09:39:25 MET 1998