Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Shells: User's Guide: HP 9000 Computers > Chapter 6 Advanced Programming

Looping

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

Many times sequential processing in a program is just not enough. We need a mechanism which will allow us to repeat the same set of commands using a different set of parameter values. To accomplish this in shell programming you can choose between three looping constructs: for, while, and until.

For

The for construct allows you to execute a set of commands once for every new value assigned to a parameter. Look at the following format:

for parameter [in wordlist]
do
command-list
done

where parameter is any parameter name, wordlist is a set of one or more values to be assigned to parameter, and command-list is a set of commands to be executed each time the loop is performed. If the word list is omitted (and also "in"), then the parameter is assigned the value of each positional parameter.

The word list is a versatile quantity in the for construct. It can be a list which you specifically type (separated with blanks), or it can be a shell command (using grave accents) which generates a list. Let's look at some examples.

for i in `ls`
do
cp $i /users/rhonda/$i
echo "$i copied"
done

This example will assign one file at a time from the current directory (the values are generated by the `ls` command) to the "i" parameter. The loop's command list will copy the file to another directory, then report the success of the copy. You can use file name generation (see “File Name Generation”) to match files. Instead of the first line of the above loop being "for i in `ls`", you could use: "for i in *".

for direc in /dev /usr /users/bin  /lib
do
num=`ls $direc | wc -w`
echo "$num files in $direc"
done

This example lists the values to be given to direc in the loop. The command list then lists each respective directory (the parameters) and assigns a word count (wc) to the num parameter. Then the word count is printed out.

for i
do
sort -d -o ${i}.srt $i
done

This final example will assign each positional parameter respectively to "i" (since the in clause was omitted). If the positional parameters are file names, the script will sort the file and place the result in a file having the same name as the unsorted file with ".srt" appended to it. It will then get the next positional parameter until all have been accessed.

(You can also use pattern matching to specify the word list. Pattern matching is discussed in “Case” and in “Using Shell Expansions”.)

While

The while construct repeatedly executes a list of commands in the following format:

while command-list1
do
command-list2
done

All of the commands in command-list1 are executed. If the last command in the list is successful (indicated by an exit status of 0 from the command), then the commands in command-list2 are executed. Then we loop back to execute command-list1 until the last command in the list is unsuccessful, and then the while loop terminates.

while [ -r "$1" ]
do
cat $1 >> composite
shift
done

This example tests the positional parameter to see if it exists and is a readable file. If it is, it appends the contents of the file to the composite file, shifts the positional parameters (what was $2 is now $1) and tests the new file. When the file is not readable, or there are no more positional parameter values ($1 is null) the while loop is terminated. (Note that without double quotes(") around $1 in the test, the test will respond with an argument expected syntax error when $1 is null.)

Until

The until construct is basically the same as the while construct except that the commands in the loop are executed until the conditions are true (instead of false like in the while loop). Here is the format:

until command-list1
do
command-list2
done

If the last command in command-list1 is unsuccessful, then the commands in command-list2 are executed. When the last command in command-list1 is successful, the until loop is terminated. Let's use the same operation in the while section to illustrate:

until [ ! -r $1 ]
do
cat $1 >> composite
done

Notice the subtle difference with the while loop. The ! negates the test conditions. We execute the loop until the condition is true (or successful). The while loop executes the commands while a condition is true (or successful).

Case

The case construct is an expansion of the if construct. If you have a condition which may have several possible responses, you can either string together many if's or you can use the case construct:

case parameter in
pattern1 [| pattern2]...) command-list1 ;;
[pattern3 [| pattern4]...) command-list2 ;;]...
esac

After the first line (which asks if parameter matches one of the following conditions) is listed all of the possibilities for parameter. Each of these lines contains a pattern (or value for parameter). The brackets ([| pattern2]...) refer to other values that may be valid. The vertical bar (|) represents "or". Finally, the patterns are followed by a close parenthesis ()), and then by a list of commands to be executed if the patterns match.

An example may better illustrate:

case $i in
-d | -r ) rmdir $dir1
echo "option -d or -r" ;;
-o ) echo "option -o" ;;
-* ) echo "incorrect response";;
esac

Here, the first positional parameter is compared to several patterns. If $1 is "-d" or "-r", then $dir1 is removed, and a statement printed. If $1 is "-o", a statement is printed. The last pattern uses the * metacharacter to "catch" all other option possibilities and flag them as errors. Remember to end each command list with ;; and to end the entire case construct with esac.

NOTE: Be aware that the order of patterns in a case is important. The specific case should precede the general case. For instance, if you used -*) before -g), there could never be any matches for the -g) because the -*) would have matched them all, already.
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-1991 Hewlett-Packard Development Company, L.P.