Control statements, Chapters 27, 29 and 31.
- If statement
if command ; then
commands
elif command ; then
commands
...
else
commands
fi
- The test is a command. It is true if it exits normally, false otherwise.
- Often used to test if a command succeeded.
- There is also a test command designed just to run tests.
- Called test or [
- Compare for string equality. if [ $resp = "yes" ]; then
- Numerical comparisons if [ $a lt $b ]; then
- File tests. if [ -r "config.txt" ]; then
- Commands can include one or more commands, separated by line breaks
or semicolons.
- While loop
while command ; do
commands
done
- Test is by exit status same as if.
- Repeats while true as in regular PLs.
- For-each loop
for var in list of strings; do
commands
done
- Executes the commands once for each member of the list.
- Sets var equal to the list value.
- The list is often either a file wild card or the argumnet
list $*.
- Case statement
case word in
pattern) commands ;;
pattern) commands ;;
...
esac
- Runs the first set of commands that matches.
- Patterns are the same as file name wildcards.
- A pattern may also consist of several wildcard expressions separated
by the | character. Matches if any do.
- Patterns are often a list of specific items followed by a * as a
default.