44.15 Handling Command-Line Arguments in Shell Scripts
To write flexible shell scripts, you usually want to give them
command-line arguments.
As you've seen in
other articles (44.11
, 44.12
)
,
44.15.1 With the $@"If you've been reading this
series (44.1
)
of articles in order, you saw the
zpg
(44.12
)
script that accepted just one command-line argument.
If you put % The third argument has a perfectly legal filename; we see more and more of them on our system - especially filesystems that are networked to computers like the Macintosh, where spaces and other "special" characters in filenames are common. Double-quoting all arguments through the script helps to be sure that the script can handle these unusual (but legal!) pathnames. In this case, we want the arguments to be passed to the gzcat command. Let's change the zpg script to read: gzcat "$@" >$temp When the shell runs the script with the arguments shown above, the command line will become: gzcat "report" "memo" "savearts/What's next?" >/tmp/zpg12345
44.15.2 With a Loop
A
for
loop (44.16
)
can step through all command-line arguments, one by one.
You can also use a
while
loop (44.10
)
that tests 44.15.3 Counting Arguments with $#
The - |
|