Previous Table of Contents Next


Controlling the Flow

You can use loops to control the flow of execution in a script. A loop is an iterative mechanism that repeats a sequence of instructions until a predetermined condition is met. You can use different forms of loops. The for/foreach loop executes a list of commands one time for each value of a loop variable. The while loop repeatedly executes a group of commands within the body of the loop until the test condition in the expression is no longer true. The Bourne and Korn shells provide an until loop that continues to execute until a command executes successfully. Table 16-14 shows the syntax for for/foreach, while, and until loops.

Table 16-14 Looping Syntax

Feature Bourne/Korn Shell C Shell
for/foreach for variable in list foreach variable (list)
loops do commands
commands end
done
while loops while command while (cond)
do commands
commands end
done
until loops until command
do
commands
done

Using for/foreach Loops

Use the for loop to process items from a fixed list or from a command-line argument. The Bourne and Korn shell for loop executes the commands between the do and the done statement as many times as there are words or strings listed after "in."

The for loop's basic syntax follows:

for variable in word1 word2 word3 . . . wordn
    command $variable
    command
done

A special format of the first line of the for loop, for variable, uses the values of positional parameters $1, $2, and so on, and is equivalent to using for variable in $@ as the first line of the for loop.

The following Bourne shell script contains two examples of for loops. The first example copies files into a backup directory. The second removes all files that contain a .o suffix.

#!/bin/sh
#
# Backup files
#
dir=/home/winsor/backup
for file in ch1 ch2 ch3 ch4
do
    cp $file $dir/${file}.back
    echo $file has been backed up in directory $dir
done

for file in *.o
do
    echo removing $file
    rm $file
done

In the C shell, use the foreach loop to process items from a fixed list or to execute commands interactively from a command line. In the C shell, the foreach construct executes a list of commands one time for each value specified in the (list).

The following C shell script contains two examples of foreach loops. The first example copies $file into a backup directory. The second example removes all files that contain a .o suffix.

#!/bin/csh -f
#
# Backup files
#
set dir=/home/winsor/backup
foreach file (ch1 ch2 ch3 ch4)
    cp $file $dir/${file}.back
    echo $file has been backed up in directory $dir
end

foreach file (*.o)
    echo removing $file
    rm $file
end

You can type the foreach loop statement at the command-line prompt. The secondary prompt (?) appears. At the prompt, type the commands you want to execute in the loop. After you complete the list of commands, type end. The commands are executed in sequence and the C shell prompt reappears. The following example displays and compiles the name of each C source file in the directory and renames the binary output file. The :r modifier removes the .c extension. If there are 10 source files in the current working directory, the loop executes 10 times. When there are no more C source files, the loop ends.

oak% foreach file (*.c)
? echo $file
? cc -o $file:r $file
? end
oak%

The following example converts raster files to GIF format, strips off the .rs suffix, and adds a .gif suffix:

oak% foreach file (*.rs)
? cat $file | rasttoppm | ppmtogif > ${file:r}.gif
? end
oak%


Previous Table of Contents Next