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
Making a for Loop with Multiple Variables

by Jerry Peek
02/10/2000

The normal Bourne shell for loop lets you take a list of items, store the items one by one in a shell variable, and loop through a set of commands once for each item:

for file in prog1 prog2 prog3
do
    ...process $file   
done

I wanted a for loop that stores several different shell variables and makes one pass through the loop for each set of variables (instead of one pass for each item, as a regular for loop does). This loop does the job:

for bunch in "ellie file16" "donna file23" "steve file34"
do
   # PUT FIRST WORD (USER) IN $1, SECOND (FILE) IN $2...
   set $bunch
   mail $1 < $2
done

If you have any command-line arguments and still need them, store them in another variable before you do that. Or, you can make the loop this way:

for bunch in "u=ellie f=file16 s='your files'" \
    "u=donna f=file23 s='a memo'" "u=steve f=file34 s=report"
do
   # SET $u (USER), $f (FILENAME), $s (SUBJECT):
   eval $bunch
   mail -s "$s" $u < $f
done

This script uses the shell's eval command to re-scan the contents of the bunch variable and store it in separate variables. Notice the single quotes like s='your files'; this groups the words for eval. The shell removes those single quotes before it stores the value into the s variable.


Back More Unix Power Tools

 




Tagged Articles

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

Recommended for You

  1. Cover of Take Control of Permissions in Mac OS X
    Take Control of Permissions in Mac OS X
    Ebook: $10.00
  2. Cover of sed and awk Pocket Reference
    sed and awk Pocket Reference
    Print: $9.95
    Ebook: $7.99
  3. Cover of Understanding Linux Network Internals
    Understanding Linux Network Internals
    Print: $49.95
    Ebook: $39.99
  4. Cover of Open Source Development with CVS
    Open Source Development with CVS
    Print: $39.99

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