Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 45.15 How to Unset all Command-Line Parameters Chapter 45
Shell Programming for the Initiated
Next: 45.17 Making a for Loop with Multiple Variables
 

45.16 Standard Input to a for Loop

An obvious place to use a Bourne shell for loop (44.16) is to step through a list of arguments - from the command line or a variable. But combine the loop with backquotes (9.16) and cat (25.2), and the loop will step through the words on standard input.

Here's an example:

for x in `cat`
do
    ...handle $x
done

Because this method splits the input into separate words, no matter how many words are on each input line, it can be more convenient than a while loop running the read command, as in article 9.20. When you use this script interactively though, the loop won't start running until you've typed all of the input; using while read will run the loop after each line of input.

- JP


Previous: 45.15 How to Unset all Command-Line Parameters UNIX Power ToolsNext: 45.17 Making a for Loop with Multiple Variables
45.15 How to Unset all Command-Line Parameters Book Index45.17 Making a for Loop with Multiple Variables

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System