O'Reilly logo

Practical UNIX by Steve Moritsugu - DTR Business Systems

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Looping

You will often have a list of items in your script that will each need to have a particular action performed on them. The for loop construct can help with this. Its general form is as follows:

for variable in list-of-items...
do
   command
   ...
done

Here, variable gets set to each value in the list-of-items, and each command is run for each variable. Here is a simple example:

for FILE in acme1 report7 /tmp/junk5
do
   lp $FILE && rm $FILE
done

The previous code will print a hard copy of the three files in the list of items: acme1, report7, and /tmp/junk5. If the lp command does not generate an error, the code will then delete the file.

You can use backquotes to generate a much larger list of items to print and then remove. In the following example, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required