While branching is an integral part of shell programming, it is only one part of program control. Looping in a program allows a portion of a program to be repeated as long as the programmer wishes. This can be for a specified number of iterations (or loops), or it can be until a particular condition is met. For instance, a programmer might want to repeat a particular operation on every file in a particular directory. Rather than rewrite the section of the program that carries out the operation over and over for each file, the operation can be written once and iterated as many times as required. Loop control also allows for programs that are more general. Rather than having to pre-specify how many iterations are required for a particular task, the programmer uses conditions to control the iterations. The Bourne shell provides a rich variety of loop control constructs. Depending on what is needed the programer can chose the construct that best suit his needs.
The for:in:do construct is used to repeat a group of commands
once for each item in a provided list.
The construct has the following form:
for VARIABLE in LIST do COMMAND LIST done
where VARIABLE is a variable name assigned each item in
LIST during the execution of COMMAND LIST.
What happens is as follows: the variable takes the value of the first
item in the list and then executes the command list; after the command
list has been passed through, the variable is assigned the value of
the second item in the list, and so on, until the list has been exhausted.
Searching for the occurence of a string in a file could be done like
the following:
#! /bin/sh
#
# A script to look for the occurence of a string in a file
# Usage: match [string] [file]
#
for word in `cat $2`
do
if [ ``$word'' = ``$1'' ]
then
echo ``Found $1 in file $2''
else
:
fi
done
where the first parameter passed to the script is string and
the second is the file thought to contain the string.
Notice that after the else statement a colon has been placed.
This is the null command which tells the computer to do nothing.
This is clearly an unnecessary section of the script and was only
added to demonstrate the use of the null command.
If the list is omitted from the for statement, each parameter
in the command line will be passed to word.
A second type of loop control construct is the while loop.
This construct, unlike the for:in:do construct, checks the TRUE
or FALSE value of a condition before proceeding. It has the following
form:
while condition do commands done
where the condition can be obtained in the usual fashion using one of
the forms of test.
The done statement signifies the end of the construct.
The following script segment counts backwards from 10 to 1:
number=10 while [ $number -ge 1 ] do echo $number number=`expr $number - 1` done
Another construct which is very similar to the while loop is
the until construct.
The construct works in precisely the same manner with the one
exception that it repeats a series of commands until a condition is met.
The until loop looks like
until condition do commands done
To count backwards, as in the above example, the until loop would be used as follows:
number=10 until [ $number -lt 1 ] do echo $number number=`expr $number - 1` done