Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

slightly confused about parameters

Status
Not open for further replies.

redsevens

IS-IT--Management
Aug 11, 2000
40
0
0
US
Hello,
I wrote a simple script that takes either two or three parameters, or '-h' or '--help' to show a friendly usage string. My test for this is as follows:

Code:
if [[ ! -z $2 || $1 -eq "-h" || $1 -eq "--help" ]]

It works correctly if I use two or three parameters or if the first parameter is '-h', but not if the first parameter is '--help'. Oddly enough, though, it displays the help message if the second parameter is '--help'. For testing purposes I am echoing "$0 $1 $2 $3" and that shows that '--help' is indeed $1 but it still fails the test. Anybody know what is going on here?
 
have you thought about approaching it like this?

#!/usr/bin/ksh

thingy=false; wotsit=false; usage=false

while $1 -ne ""
do
param=$1; shift
if [[ $param -eq -thingy ]]
then
thingy=true
fi
if [[ $param -eq -wotsit ]]
then
wotsit=true
fi
if [[ $param -eq -h ]]
then
usage=true
fi
if [[ $param -eq --help ]]
then
usage=true
fi
done
if [[ $usage -eq true ]]
then
echo "usage message"
exit
fi Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 

Use = operator for string test
Put substitutions betwen "

if [[ ! -z "$2" || "$1" = "-h" || "$1" = "--help" ]] Jean Pierre.
 
ok,
Here is one more suggestion, You will notice I avoided multiple parameters:

#!/usr/bin/ksh
if [ $1 = help ] # if use help
then
clear

echo "Usage: $0 [help]"
echo
echo "help Show this text"
echo
echo &quot;<blank> This will run the program;&quot;
echo
echo &quot;<change> This will change the program test&quot;
echo
echo &quot;<any thing else> This will grep for that&quot;
echo
echo &quot;For any suggestion of future releases please&quot;
echo &quot;send e-mail to the Dev team at&quot;

exit 1
fi

if [ $1 = change ]
then

# EDIT PROGRAM GOES HERE
exit 1
fi
if [ -z &quot;$1&quot; ]
then
#PROGRAM GOES HERE
else
clear

#PROGRAM HERE WITH GREP |grep -i $1

fi


Good luck
 
Thanks to all three for the contributions - Jean Pierre gets the prize for being my favorite solution, while Squash wins an honorary mention for jumping to the most conclusions. :) The other two solutions wouldn't work because $1 should be a directory and $2 is a file, so a switch-type structure is inappropriate (I am doing an
Code:
if [ -d $1 ]
and
Code:
if [ -f $2 ]
later on in the script).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top