Operating System - Linux
1752808 Members
5527 Online
108789 Solutions
New Discussion юеВ

Re: Parameter checking in scripts

 
SOLVED
Go to solution
PJJ
Frequent Advisor

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.
17 REPLIES 17
Peter Godron
Honored Contributor

Re: Parameter checking in scripts

Pete,
you could use the length of $1:
a=`expr length "$1"`
or
if [ -z "$1" ]
Hemmetter
Esteemed Contributor

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


James R. Ferguson
Acclaimed Contributor

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...
Peter Nikitka
Honored Contributor

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"
PJJ
Frequent Advisor

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???
PJJ
Frequent Advisor

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
James R. Ferguson
Acclaimed Contributor

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...
PJJ
Frequent Advisor

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!
Hein van den Heuvel
Honored Contributor

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.