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

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 Samba Pocket Reference
    Samba Pocket Reference
    Print: $9.95
  2. Cover of Essential CVS
    Essential CVS
    Print: $44.99
    Ebook: $35.99
  3. Cover of Linux System Administration
    Linux System Administration
    Print: $44.99
    Ebook: $35.99
  4. Cover of Debian System
    Debian System
    Print: $44.95

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, 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