Previous Table of Contents Next


Using while Loops

Use while loops to repeatedly execute a group of commands within the body of a loop until the test condition in the expression is no longer true. In other words, the while loop says, "While the expression is true, execute these commands."

For the Bourne and Korn shells, the while loop executes the commands between the do and the done statements as long as the initial command exits with a status of zero.

The basic syntax for the while loop follows:

while command
do
    command
    command
done

In the following Bourne shell script, the first example sets the variable num equal to 0 and adds 1 to it as long as the number is less than or equal to 4.

#!/bin/sh

num=Ø
while [ $num -le 4 ]
do
        num=`expr $num + 1`
        echo number: $num
done

Refer to "Mathematical Operations" for information on how to use the expr commmand.

The next example displays the command-line arguments in sequence when any arguments are given. Refer to the section "Shifting Command-Line Arguments" earlier in this chapter for information about the shift command.

#!/bin/sh
while [ $# -ne Ø ]
do
        echo argument: $1
        shift
done

For the C shell, the while loop executes the commands between the while and the end statement as long as the expression evaluates to true.

The basic syntax for the while loop follows:

while (expression)
    command
    command
end

In the following C shell script, the first example sets the variable num equal to 0 and adds 1 to it as long as the number is less than or equal to 4:

#!/bin/csh -f

@ num = Ø
while ($num <= 4)
    @ num++
    echo "number: $num"
end

The output of this script follows:

oak% tryit
number: Ø
number: 1
number: 2
number: 3
number: 4

The next example displays the command-line arguments in sequence when any arguments are given:

#!/bin/csh -f
#
while ($#argv != Ø)
        echo "argument: $1"
        shift
end

Using Until Loops

Use Bourne or Korn shell until loops to test an expression until the command returns a successful status. The syntax for the until loop is similar to the while loop:

until command
do
    command
    command
done


NOTE:  The until condition is checked at the top of the loop, not at the bottom.

The following example prints out the numbers 1 through 5:

#!/bin/sh

num=Ø
until [ $num -gt 4 ]
do
        num=`expr $num + 1`
        echo number: $num
done

The results of this script are the same as the example shown using the while loop. Refer to the section "Mathematical Operations" on page 349 for a description of the expr command used in the preceding example.

Breaking Loops

All three shells have built-in break and continue commands that you can use to break out of a for/foreach, while, or until loop. The break command forces premature termination of the loop and transfers control to the line after the done (for the Bourne and Korn shells) or end (for the C shell) statement. In the following C shell example, the script continues running the while loop until the user answers Yes (or yes, y, or Y). When the user does answer yes, the break terminates the while loop and, in this case, exits because there are no additional commands to execute.

#!/bin/csh -f
#
while (1)
        echo "Finished yet? \c"
        set answer = $<
        if ($answer =~ [Yy]*) break
end

You can use the continue command in a similar way to control for/foreach and while loops. The continue command operates on the innermost loop with which it is associated. The continue statement transfers execution immediately back to the while test, skipping all subsequent commands in the innermost loop. If continue is used with a foreach loop, the continue statement quits the current iteration of the loop and processes the next item in the list.


Previous Table of Contents Next