Bash scripting question
Condon Thomas A KPWA
tcondon
Mon May 17 11:47:52 PDT 2004
David,
>> for param in $*
>> do
>> echo $param
>> done
>
> Thanks to you and also David Bandel for the help. I was hoping for a
> way in which I could reference a particular argument by number, but I
> suppose that I can accomplish the same by keeping a separate counter.
Bash has a shift command to shift each of the command line variables left
(use one, shift all, use one, shift all) that allowed you to treat each one
in turn using the same number. For instance (shamelessly lifted):
---------------------------------
#!/bin/bash
# Using 'shift' to step through all the positional parameters.
# Name this script something like shft,
#+ and invoke it with some parameters, for example
# ./shft a b c def 23 skidoo
until [ -z "$1" ] # Until all parameters used up...
do
echo -n "$1 "
shift
done
echo # Extra line feed.
exit 0
---------------------------------
I'm also pretty certain (but again memory fails on specifics) that you can
create a test to see if the 'param' (no '$' -- unexpanded) in the first
section of code above is equal to a certain number and if so do one thing
with it and if not do another.
You can always reference the parameters by number. $3 would be the third
argument after the command on the command line, unless you have shifted or
set it to some value in the script. In csh you could address the arguments
as $argv[i], too. For instance, $argv[4] == $4. This allowed for use of a
series of the arguments. parms[] = $argv[3-$#argv] would set parms to an
array of arguments 3-n where n=$#, or the last argument. argv, of course,
let you access more than just the first ten arguments.
You have probably been there, but I can recommend the following links:
http://www.helpfixmypc.com/bash.htm
http://www.beforever.com/bashtut.htm
http://www.tldp.org/LDP/abs/html/
HTH.
In Harmony's Way, and In A Chord,
Tom :-})
Thomas A. Condon
Barbershop Bass Singer
Registered Linux User #154358
A Jester Unemployed
More information about the Linux-users
mailing list