LinuxDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


Unix Power Tools
Standard Input to a for Loop

by Jerry Peek
02/10/2000

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


Back More Unix Power Tools

 




Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!


Linux Resources
  • Linux Online
  • The Linux FAQ
  • linux.java.net
  • Linux Kernel Archives
  • Kernel Traffic
  • DistroWatch.com


  • Sponsored by: