Re: Can $1 be set in a script?
- From: larryalk <nobody@xxxxxxxxxxx>
- Date: Thu, 30 Mar 2006 23:03:24 GMT
On Thu, 30 Mar 2006 13:44:57 -0900, floyd@xxxxxxxxxx (Floyd L.
Davidson) wrote:
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.
Thanks for your answer Floyd and I apologize for still being unclear.
Whether $1 and $2 already exist I, wish to assign a new variable to
them so I can use $1 and $2 in an existing_script.
The only script modification would be editing the variables at the top
which is better than re-working the entire script.
Larry
--
My real sig is much better.
.
- Follow-Ups:
- Re: Can $1 be set in a script?
- From: Floyd L. Davidson
- 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
- Re: Can $1 be set in a script?
- From: Floyd L. Davidson
- 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
|