home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Unix Power ToolsUnix Power ToolsSearch this book

36.11. Standard Input to a for Loop

An obvious place to use a Bourne shell for loop (Section 35.21) is to step through a list of arguments -- from the command line or a variable. But combine the loop with backquotes (Section 28.14) and cat (Section 12.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. 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



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.