-->
Previous Table of Contents Next


Using Iterative Structures

Iterative control structures allow you to write shell scripts that contain loops. The two basic types of loops are for and while loops.

With for loops, you specify a collection of files or values to use with some commands. To copy all the files whose names end with the characters .txt to the directory textdir, for example, use the following for loop:


for i in *.txt

do

       cp $i textdir/$i

done

The shell interprets the statement for i in *.txt and allows the variable i to take on the name of any file in the current directory whose name ends with .txt. You can then use the variable $i with any statements between the do and the done keywords.

The script in Listing 18.6 prints a collection of files, each with its own banner page. It also sends mail to the user concerning the status of the print requests. The characters $* represent all the parameters given to the shell command.

Listing 18.6 Processing Files with the for Command


# Name:      Prntel

# Purpose:   Print one or more files

#            each with own title page

#             Notify user which files were sent to the printer

#            and which were not.

#             Do this for all parameters to the command

for i in $*

do

       if lp -t $i -dlasers $i > /dev/null

      then

            echo $i >> printed

      else

            echo $i >> notprinted

      fi

done

# end of loop

if test -s printed

then

       echo “These files were sent to the printer ” > mes

      cat printed >> mes

     mail $LOGNAME < mes

      rm mes printed

f i

if test -s notprinted

then

      echo “These files were not sent to the printer ” >mes

     cat notprinted >> mes

    mail $LOGNAME < mes

     rm mes notprinted

fi

A while loop looks at the exit status of a command in the same way the if statement looks at the status. The script in Listing 18.7 notifies users when they’ve received new mail. The script makes the assumption that if a mailbox changes, a user has new mail. The script uses the command diff to compare two files and then reports on the differences. If the files are the same, the exit status is zero (the command is successful).

Listing 18.7 Repeating Commands with while


# Name:        checkmail

# Purpose:      Notify user if their mail box has changed

# Suggestion:  Run this in the background

# get a size of mail box for comparison

      cp $MAIL omail         # Get set for first time through

# MAIL is a “special” variable indicating the user’s mailbox

# while omail and $MAIL are the same, keep looping

      while diff omail $MAIL > /dev/null

     do

           cp $MAIL omail

             sleep 30          # sleep, pause for 30 seconds

     done

# There must be a change in the files

    echo “New mail!!” | write $LOGNAME

You can see that some of the commands and concepts used with if…then…else statements can be transferred to while loops. The difference, of course, is that with while loops, you’re dealing with an iterative, repetitive process.

Customizing Linux Shells

The shell starts when you log in. Tables 18.2 and 18.3 show you that special variables are given values by the shell to help define your shell environment. The shell sets some of these variables. You can change these settings and give other variables values by editing the file .profile if you’re using the Bourne or bash shell. If you’re using the C shell, you set the variables by editing the file .login. You can also use command aliasing to define aliases for commands.

Whenever you issue a command, a new shell starts; it inherits many of the characteristics—or much of the environment—of the existing shell. Note these two things about the new shell:

  The new shell runs in your current directory. The pwd command returns the same value within a shell as it gives before the shell was started.
  The new shell receives many of its variables from the existing shell. There are ways to make sure that variables set in the existing shell are exported to the new shell.


Previous Table of Contents Next