- for
The for command will loop on each word and execute the commands in the do/done part.
for var in word word word...
do
...
done
- Filename substitution
You can do filename substitution in a word list:
for var in *
do
...
done
will use the list of all matching files as the word list for the loop. You can use any of the
standard wildcard characters ( * ? [...]) the same way you would use them on the command line.
- $@
The $@ variable is almost like the $* variable except that when enclosed in double quotes it treats
each argument as if it is individually quoted. Eg.
"$*" = "a b c"
"$@" = "a" "b" "c"
- Automatic arguments
If you omit the in and the word list it is the same as in "$@". Eg.
for var
do
...
done
- while
The while loop will loop as long as the command has a zero exit status.
while command
do
command
command
...
done
- until
The until command will loop as long as the command has a non-zero exit status. (Until true)
until command
do
command
command
...
done
- break
The break command will end the loop and start executing
commands following the done command. break can have an integer
argument which indicates how many levels of loops to break out of.
- continue
The continue command will end the current loop only and
start at the top of the next iteration if the condition is still okay.
continue can also have an integer
argument which indicates which level of nested loops to go back to the begining of.
- loop I/O
The loop can have I/O redirected as a whole by placing redirection or piping
after the done command. Any output in the loop will be redirected or
piped as indicated by the done.
- getopts
The getopts command can be use to extract otions from the commandline.
The first argument is the list of acceptable options.
A colon in the list indicates that the option is followed by a value. If it is,
it is assigned to the variable OPTARG.
The second agument is the name of a variable to assign the option. If the option is
not in the list, it is set to '?' and an error message is sent to stderr.
while getopts mt: option
do
case "$option" in
m) mailopt=TRUE;;
t) interval=$OPTARG;;
\?) echo "Usage: mon [-m] [-t n] user"
exit 1;;
esac
done