-->
Previous | Table of Contents | Next |
The shell languages also provide several iteration or looping statements. The most commonly used of these is the for statement. In addition to the for loop, there are several others (such as while and until) but they are all variations of the same approach. The forloop is by far the most commonly used in shell programs.
The for statement executes the commands that are contained within it a specified number of times. bash and pdksh have two variations of the for statement. The for statement syntax is the same in both bash and pdksh.
The first form of for statement that bash and pdksh support has the following syntax:
for var1 in list do commands done
In this form, the for statement executes once for each item in the list. This list can be a variable that contains several words separated by spaces or it can be a list of values that is typed directly into the statement. Each time through the loop, the variable var1 is assigned to the current item in the list until the last one is reached.
The second form of for statement has the following syntax:
for var1 do statements done
In this form, the for statement executes once for each item in the variable var1. When this syntax of the for statement is used, the shell program assumes that the var1 variable contains all the positional parameters that were passed into the shell program on the command line.
Typically, this form of for statement is the equivalent of writing the following for statement:
for var1 in $@ do statements done
The equivalent of the for statement in tcsh is called the foreach statement. It behaves in the same manner as the bash and pdksh for statement. The syntax of the foreach statement is the following:
foreach name (list) commands end
The following is an example of the bash or pdksh style of for statement. This example takes as command-line options any number of text files. The program reads in each of these files, converts all the letters to uppercase, and then stores the results in a file of the same name but with a .caps extension.
for file do tr a-z A-Z < $file >$file.caps done
The same example written in tcsh shell language is shown next:
# foreach file ($*) tr a-z A-Z < $file >$file.caps end
Another iteration statement offered by the shell programming language is the while statement. This statement causes a block of code to be executed while a provided conditional expression is true. The syntax for the while statement in bash and pdksh is the following:
while expression do statements done
The syntax for the while statement in tcsh is the following:
while (expression) statements end
The following is an example of the bash and pdksh style of while statement. This program lists the parameters that were passed to the program, along with the parameter number.
count=1 while [ -n $* ] do echo This is parameter number $count $1 shift count=expr $count + 1 done
As you will see in the section titled The shift Command, the shift command moves the command-line parameters over one space to the left.
The same program written in the tcsh language is shown next:
# set count = 1 while ( $* != ) echo This is parameter number $count $1 shift set count = expr $count + 1 end
The until statement is very similar in syntax and function to the while statement. The only real difference between the two is that the until statement executes its code block while its conditional expression is false, and the while statement executes its code block while its conditional expression is true. The syntax for the until statement in bash and pdksh is
until expression do commands done
The same example that was used for the while statement can be used for the until statement. All you have to do to make it work is negate the condition. This is shown in the following code:
count=1 until [ -z $* ] do echo This is parameter number $count $1 shift count=expr $count + 1 done
The only difference between this example and the while statement example is that the -n test command option (which means that the string has nonzero length) was removed, and the -z test option (which means that the string has zero length) was put in its place.
In practice, the until statement is not very useful because any until statement you write can also be written as a while statement. tcsh does not have an equivalent of the until statement other than rewriting it as a while loop.
Previous | Table of Contents | Next |