LinuxDevCenter.com

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things Linux across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to Linux Subscribe to Newsletters
Linux & Unix > Excerpts >

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

 




Tagged Articles

Be the first to post this article to del.icio.us

Recommended for You

  1. Cover of Linux Unwired
    Linux Unwired
    Print: $24.95
  2. Cover of High Performance Linux Clusters with OSCAR, Rocks, OpenMosix, and MPI
    High Performance Linux Clusters with OSCAR, Rocks, OpenMosix, and MPI
    Print: $39.95
    Ebook: $31.99
  3. Cover of Power Programming with RPC
    Power Programming with RPC
    Print: $29.95
  4. Cover of Book of Webmin
    Book of Webmin
    Print: $34.95

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2010, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com