DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with the UNIX system shell

Loop constructs: for and while

In the previous examples in this section, the commands in shell programs have been executed in sequence. The for and while looping constructs allow a program to execute a command or sequence of commands several times.

The for loop

The for loop executes a sequence of commands once for each member of a list. It has the format shown in ``Format of the for loop construct''.

   for variable
         in a_list_of_values
   do
        command_1
        command_2
            .
            .
            .
        last_command
   done

Format of the for loop construct

For each iteration of the loop, the next member of the list is assigned to the variable given in the for clause. References to that variable may be made anywhere in the commands within the do clause.

It is easier to read a shell program if the looping constructs are visually clear. Because the shell ignores spaces at the beginning of lines, each section of a command can be indented as it was in the above format. Also, if you indent each command section, you can easily check to make sure each do has a corresponding done at the end of the loop.

The variable can be any name you choose. For example, if you call it var, then the values given in the list after the keyword in will be assigned in turn to var; references within the command list to $var will make the value available. If the in clause is omitted, the values for var will be the complete set of arguments given to the command and available in the special parameter ``$*''. The command list between the keywords do and done will be executed once for each value.

When the commands have been executed for the last value in the list, the program will execute the next line below done. If there is no line, the program will end.

The easiest way to understand a shell programming construct is to try an example. Create a program that will move files to another directory. Include the following commands for the purposes shown.


echo
Prompt the user for a pathname to the new directory.

read
Assign the pathname to the variable path.

for variable
Call the variable file; it can be referenced as $file in the command sequence.

in list_of_values
Supply a list of values. If the in clause is omitted, the list of values is assumed to be ``$*'' (all the arguments entered on the command line).

do command_sequence
Provide a command sequence. The construct for this program will be:
   do
       mv $file $path/$file
   done
The following screen shows the text for the shell program mv.file:
   $ cat mv.file
   echo Please type in the directory path
   read path
   for file
      in memo1 memo2 memo3
   do
      mv $file $path/$file
   done
   $
In this program the values for the variable file are already in the program. To change the files each time the program is invoked, assign the values using positional parameters or the read command. When positional parameters are used, the in keyword is not needed, as the next screen shows:
   $ cat mv.file
   echo type in the directory path
   read path
   for file
   do
      mv $file $path/$file
   done
   $
You can move several files at once with this command by specifying a list of filenames as arguments to the command. (This can be done most easily using the filename expansion mechanism described earlier).

The while loop

Another loop construct, the while loop, uses two groups of commands. It will continue executing the sequence of commands in the second group, the do . . . done list, as long as the final command in the first group, the while list, returns a status of (true), meaning the statements after the do can be executed.

The general format of the while loop is shown in ``Format of the while loop construct''.

   while
        command_1
            .
            .
            .
        last_command
   do
        command_1
            .
            .
            .
        last_command
   done

Format of the while loop construct

For example, a program called enter.name uses a while loop to enter a list of names into a file. The program consists of the following command lines:

   $ cat enter.name
   while
      read x
   do
      echo $x>>xfile
   done
   $
With some added refinements, the program becomes:
   $ cat enter.name
   echo "Please type in each person's name and then a <Return>"
   echo "Please end the list of names with a CTRL-d"
   while read x
   do
      echo $x>>xfile
   done
   echo xfile contains the following names:
   cat xfile
   $
Notice that, after the loop is completed, the program executes the commands below the done.

You used special characters in the first two echo command lines, so you must use quotes to turn off the special meaning. The next screen shows the results of enter.name:

   $  enter.name
   Please type in each person's name and then a <Return>
   Please end the list of names with a CTRL-d

Mary Lou
Janice
<CTRL-d> xfile contains the following names: Mary Lou Janice $
Notice that after the loop completes, the program prints all the names contained in xfile.

The shell's garbage can: /dev/null

The file system has a file called /dev/null where you can have the shell deposit any unwanted output.

Try /dev/null by ignoring the results of the who command. First, type in the who command. The response tells you who is on the system. Now, try the who command, but redirect the output into /dev/null:

   who > /dev/null
Notice the system responded with a prompt. The output from the who command was placed in /dev/null and was effectively discarded.
Next topic: Conditional constructs: if and case
Previous topic: Using return codes with the exit command

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004