Shell flow-of-control syntax
- Simple alternative selection:
- if command
then commands
else other-commands
fi
- Multi-way selection:
- case word in
item) commands ;;
esac
item may be a "glob" wildcard match, so *)
can be used as a default match
- Looping:
- while command
do commands
done
for var in list
do commands
done
(var is sequentially assigned to each item in list)
If a command exits with status 0, this is considered "true" for
if and while; non-zero status is "false".
The test construct test -option args or
[ -option args ] allows for some general
conditional tests, such as (but not limited to, see man test):
- -f file: does a named file exist?
- -d dir: does a named directory exist?
- -n "$var": is the value of a variable non-empty?
- -z "$var": is the value of a variable empty?
There are lots of other tests, including logical operators and string
or numeric comparisons.
Next ->
Steve VanDevender
Last modified: Wed Jul 7 14:38:19 PDT 2004