Re: Can $1 be set in a script?
- From: floyd@xxxxxxxxxx (Floyd L. Davidson)
- Date: Thu, 30 Mar 2006 13:44:57 -0900
larryalk <nobody@xxxxxxxxxxx> wrote:
To be clearer, assuming a script called with or without a $1
how can I can assign the (possibly non-existant) $1 to something?
Instead of
something=${1}
Do this,
if [ -z "${1}" ]
then
something="default string"
else
something="${1}"
fi
Ahem..., you said set $1 to something, and I'm saying set
something to $1 and then use $something everywhere in your
script that you would have used $1.
Or, you might want to do this instead (slightly different
results),
if [ 1 -gt ${#} ]
then
something="default string"
else
something="${1}"
fi
The first script will use the default if $1 is an empty string,
which means the script cannot intentionally be invoked with an
empty string, like this,
$ ./foo "" two three four
The second variation will only replace the original $1 if there
are *no* arguments. It would have to be invoked like this,
$ ./foo
to get the defaulted value for $something.
Note that the above use of brackets, {...}, is arbitrary and
makes no difference in the examples as written; however, the use
of double quotes, "...", is significant.
--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@xxxxxxxxxx
.
- Follow-Ups:
- Re: Can $1 be set in a script?
- From: Bill Marcum
- Re: Can $1 be set in a script?
- From: larryalk
- Re: Can $1 be set in a script?
- References:
- Can $1 be set in a script?
- From: larryalk
- Re: Can $1 be set in a script?
- From: Chris F.A. Johnson
- Re: Can $1 be set in a script?
- From: larryalk
- Can $1 be set in a script?
- Prev by Date: Re: Can $1 be set in a script?
- Next by Date: Re: Can $1 be set in a script?
- Previous by thread: Re: Can $1 be set in a script?
- Next by thread: Re: Can $1 be set in a script?
- Index(es):
Relevant Pages
|