Re: Parameter checking in scripts (248 Views)
Reply
Frequent Advisor
PJJ
Posts: 47
Registered: ‎08-24-2004
Message 1 of 18 (248 Views)
Accepted Solution

Parameter checking in scripts

Hi,
I need to write a script which performes some action on files which are 4 weeks old (no parameters specified), unless another timeframe is specified (in parameter $1).
I can't figure out how to check if $1 is specified or not.
Any help appreciated.
Pete.
Please use plain text.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 2 of 18 (248 Views)

Re: Parameter checking in scripts

Pete,
you could use the length of $1:
a=`expr length "$1"`
or
if [ -z "$1" ]
Please use plain text.
Esteemed Contributor
Hemmetter
Posts: 284
Registered: ‎10-17-2000
Message 3 of 18 (248 Views)

Re: Parameter checking in scripts

Hi Peter

see:
man ksh(1) Section "Parameter Substitution."

<...>
${parameter:=word} If parameter is not set or is null, set it to
word; then substitute the value of the
parameter. Positional parameters cannot be
assigned in this way.
<...>
my be what you're searching for.



rgds
HGH


Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 18 (248 Views)

Re: Parameter checking in scripts

Hi Pete:

One way is simply to check the number of arguments passed to the script:

# cat .mysh
#!/usr/bin/sh
if [ $# -eq 0 ]; then
echo "nothing passed"
else
echo "I passed $@"
fi

# ./mysh 123
I passed 123

# ./mysh
nothing passed

Regards!

...JRF...
Please use plain text.
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 5 of 18 (248 Views)

Re: Parameter checking in scripts

Hi,

set your script variable conditionally:

...
timespec=${1:-28}
...
find .. -mtime +$timespec ...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Please use plain text.
Frequent Advisor
PJJ
Posts: 47
Registered: ‎08-24-2004
Message 6 of 18 (248 Views)

Re: Parameter checking in scripts

Thanks all for the quick replies.
James's solution seems elegant. But when I run this script with parameters it gives correct output. Running a 2nd time without parameters it gives the same output as the 1st run.
Somehow it seems to conserve the parameters???
Please use plain text.
Frequent Advisor
PJJ
Posts: 47
Registered: ‎08-24-2004
Message 7 of 18 (248 Views)

Re: Parameter checking in scripts

Sorry, I shpuld have supplied output:
$ . script.sh a b c
I passed a b c
$ . script.sh
I passed a b c
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 18 (248 Views)

Re: Parameter checking in scripts

Hi (again) Pete:

Well, that's quite impossible for the script to retain state between executions. I suspect that your terminal settings are to blame. I suspect that you ran the script with arguments; recalled the command line; and "backspaced" over the arguments. If the backspace truly didn't erase, then you would see the behavior you did.

Regards!

...JRF...
Please use plain text.
Frequent Advisor
PJJ
Posts: 47
Registered: ‎08-24-2004
Message 9 of 18 (248 Views)

Re: Parameter checking in scripts

Hi James,

I didn't recall... Is it the way I call the script?
$ ./script.sh
nothing passed
$ ./script.sh a b c
I passed a b c
$ ./script.sh
nothing passed
$ . script.sh
I passed a b c
$ . script.sh d e f
I passed d e f
$ . script.sh
I passed d e f

This last run not recalled!
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,585
Registered: ‎05-19-2003
Message 10 of 18 (248 Views)

Re: Parameter checking in scripts

Peter,

Yes your are 'sourcing' the script instead of simply running it.
Is that deliberate? (why?)
What shell are you in while executing this?
Did you use the exact script as provided, or did you already augment it?
This does not reproduce for me.

From the Posix shell man page:
"The dot (.) special command, as in . file, reads the entire file before any commands are executed. Therefore, alias and unalias commands in the file will not apply to any functions defined in the file."


Groetjes,
Hein.
Please use plain text.
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 11 of 18 (248 Views)

Re: Parameter checking in scripts

Hi,

your error is using
. myscript

This is executed directly in the running shell.
Check this:
. myscript a b c

echo $1
a

You have to call the script 'in normal way':

myscript a b c

without leading '. '.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 12 of 18 (248 Views)

Re: Parameter checking in scripts

Hi Pete:

Both Hein and Peter N. are correct. A dot character followed by a space followed by the script name means *read* or 'source' the file.

This is used to bring environmental variables into your current environment (shell). For instance, do this:

# cat /tmp/env
#!/usr/bin/sh
export VAR1=jrf
export VAR2=hein
export VAR3=peter

Now, compare:

# /tmp/env
echo "${VAR1}, ${VAR2} ${VAR3}"
sh: VAR1: Parameter not set.

...to:

# . /tmp/env
echo "${VAR1}, ${VAR2} ${VAR3}"
jrf, hein peter

Regards!

...JRF...
Please use plain text.
Honored Contributor
Bill Hassell
Posts: 14,203
Registered: ‎05-29-2000
Message 13 of 18 (248 Views)

Re: Parameter checking in scripts

As James points out, 'sourcing' a script means that the current shell remembers any variables that are previously set. So to allow for maximum flexibility, you should always test for the quantity of parameters passed to the script and ignore anything in the variables if the quantity is not correct (or in your case, preset the default values):

#!/usr/bin/sh
set -u

[ $# -eq 1 ] && VAR1=$1 || VAR1=4

echo "VAR1 is $VAR1"

In this case, the test is explicitly for one parameter so anything else (ie, no parameters, two parameters, etc) will leave VAR1=4, otherwise, VAR1 is assigned the first parameter on the command line. As with all interactive scripts, you should assume that monkeys are typing on the keyboard and the value for VAR1 should be tested for validity (ie, not a number, number too large or small, etc)
Please use plain text.
Frequent Advisor
PJJ
Posts: 47
Registered: ‎08-24-2004
Message 14 of 18 (248 Views)

Re: Parameter checking in scripts

I'm lost now, seeing no diff in your explanation.
Using /usr/bin/sh :
$ more /tmp/env
#!/usr/bin/sh
echo "${VAR1}, ${VAR2} ${VAR3}"

$ export VAR1=jrf
$ export VAR2=hein
$ export VAR3=peter

$ /tmp/env
jrf, hein peter
$ . /tmp/env
jrf, hein peter
Please use plain text.
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 15 of 18 (248 Views)

Re: Parameter checking in scripts

Hi,

JRF's solutions was (sorry) a bit misleading, because the variables VARi should NOT exported in the calling shell.

Let us try my way:
cat myscript
#!/usr/bin/sh
v1=${1:-jrf}
v2=${2:-hein}
v3=${3:-nik}
echo v1=$v1 v2=$v2 v3=$v3

Now try:
myscript
v1=jrf v2=hein v3=nik

myscript set your params
v1=set v2=your v3=params

. myscript set your params
v1=set v2=your v3=params

. myscript
v1=set v2=your v3=params

myscript
v1=jrf v2=hein v3=nik

set try this
. myscript
v1=try v2=this v3=nik

But you always get your desired result via
myscript Jong
v1=Jong v2=hein v3=nik

mfG Peter




The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 16 of 18 (248 Views)

Re: Parameter checking in scripts

Hi (again):

THe 'echo' is run after either running the script or sourcing it. It wasn't part of the script I meant you to run. Eliminating the superflous (yes, misleading) 'export', what I showed was:

#!/usr/bin/sh
VAR1=jrf
VAR2=hein
VAR3=peter
exit

Now, compare:

# /tmp/env #...run the script
# echo "${VAR1}, ${VAR2} ${VAR3}"
sh: VAR1: Parameter not set.
# set #...to see the environmental variables
...to:

# . /tmp/env #...source the script
# echo "${VAR1}, ${VAR2} ${VAR3}"
jrf, hein peter
# set #...now shows VAR1, VAR2 and VAR3 too!

Regards!

...JRF...
Please use plain text.
Frequent Advisor
PJJ
Posts: 47
Registered: ‎08-24-2004
Message 17 of 18 (248 Views)

Re: Parameter checking in scripts

With all this help the 'global-' or 'localness' is clear now.

And for the parameterquestion this is elegant: v1=${1:-jrf}

Thanks all!
Pete
Please use plain text.
Frequent Advisor
PJJ
Posts: 47
Registered: ‎08-24-2004
Message 18 of 18 (248 Views)

Re: Parameter checking in scripts

Closed, thank to all.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation