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
Handling Command-Line Arguments with a for Loop

by Jerry Peek
02/10/2000

Sometimes you want a script that will step through the command-line arguments one by one. (The $@ parameter gives you all of them at once.) The Bourne shell for loop can do this. The for loop looks like this:

for arg in list
do
    ...handle $arg...
done

If you omit the in list, the loop steps through the command-line arguments. It puts the first command-line argument in arg (or whatever else you choose to call the shell variable), then executes the commands from do to done. Then it puts the next command-line argument in arg, does the loop... and so on... ending the loop after handling all the arguments.

For an example of a for loop, let's hack on the the zpg script.

#!/bin/sh
# zpg - UNCOMPRESS FILE(S), DISPLAY WITH pg
# Usage: zpg [pg options] file [...files]
stat=1  # DEFAULT EXIT STATUS; RESET TO 0 BEFORE NORMAL EXIT
temp=/tmp/zpg$$
trap 'rm -f $temp; exit $stat' 0
trap 'echo "`basename $0`: Ouch! Quitting early..." 1>&2' 1 2 15

files=  switches=
for arg
do
    case "$arg" in
    -*) switches="$switches $arg" ;;
    *)  files="$files $arg" ;;
    esac
done

case "$files" in
"") echo "Usage: `basename $0` [pg options] file [files]" 1>&2 ;;
*)  for file in $files
    do gzcat "$file" | pg $switches
    done
    stat=0
    ;;
esac

We added a for loop to get and check each command-line argument. For example, let's say that a user typed:

% zpg -n afile ../bfile

The first pass through the for loop, $arg is -n. Because the argument starts with a minus sign (-), the case treats it as an option. Now the switches variable is replaced by its previous contents (an empty string), a space, and -n. Control goes to the esac and the loop repeats with the next argument.

The next argument, afile, doesn't look like an option. So now the files variable will contain a space and afile.

The loop starts over once more, with ../bfile in $arg. Again, this looks like a file, so now $files has afile ../bfile. Because ../bfile was the last argument, the loop ends; $switches has the options and $files has all the other arguments.

Next, we added another for loop. This one has the word in followed by $files, so the loop steps through the contents of $files. The loop runs gzcat on each file, piping it to pg with any switches you gave.

Note that $switches isn't quoted. This way, if $switches is empty, the shell won't pass an empty argument to pg. Also, if $switches has more than one switch, the shell will break the switches into separate arguments at the spaces and pass them individually to pg.

You can use a for loop with any space-separated (actually, IFS-separated) list of words -- not just filenames. You don't have to use a shell variable as the list; you can use command substitution (backquotes), shell wildcards, or just "hardcode" the list of words:

for person in Joe Leslie Edie Allan
do
   echo "Dear $person," | cat - form_letter | lpr
done

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 Annoyances for Geeks
    Linux Annoyances for Geeks
    Print: $34.99
    Ebook: $27.99
  2. Cover of Linux in a Windows World
    Linux in a Windows World
    Print: $44.95
  3. Cover of Linux Desktop Hacks
    Linux Desktop Hacks
    Print: $24.95
    Ebook: $19.99
  4. Cover of Managing RAID on Linux
    Managing RAID on Linux
    Print: $39.95
    Ebook: $31.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