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, ...
Get Practical UNIX now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.