Re: Can $1 be set in a script?



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
.



Relevant Pages

  • Re: Date & time validation
    ... The script takes values from a large file containing various data sets, ... I need to validate these dates and time ... def dateCheck ... true otherwise return an empty string ...
    (comp.lang.ruby)
  • Date & time validation
    ... The script takes values from a large file containing various data sets, ... I need to validate these dates and time ... I need to know does it match the date format of yyyy/mm/dd, ... true otherwise return an empty string ...
    (comp.lang.ruby)
  • Re: Can $1 be set in a script?
    ... assuming a script called with or without a $1 ... how can I can assign the (possibly non-existant) $1 to something? ... Ahem..., you said set $1 to something, and I'm saying set ... The first script will use the default if $1 is an empty string, ...
    (comp.os.linux.x)
  • Re: Finding "this file name".
    ... There is no script, ... coming from a -e command line switch. ... results in an empty string, as Windows has know knowledge of any path ...
    (perl.beginners)
  • Re: sending variable to include file
    ... vetchling wrote: ... problem I can't fix, ... being nothing but an empty string. ... You don't need to pass a variable to an included file, it already has access to all variables in the includING script. ...
    (comp.lang.php)