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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding options to a script

Status
Not open for further replies.

johngiggs

Technical User
Oct 30, 2002
492
US
I tried adding the following lines to my script, but when a value is not specified for positional parameter 1, an error is displayed.

if [ $1 = "help" ] || [ $1 = '-h' ]
then
echo "\nHELP\n==========\n
echo "Help commands"
...........
exit
else
continue
fi

The error is: ./TNG-job[3]: test: argument expected

How can I make this work without displaying an error message? Any help would be greatly appreciated.

Thanks,

John
 
Use double quotes around $1

if [ "$1" = "help" ] || [ "$1" = '-h' ]
 
If this is for a menu, a 'nicer way to do things is

function draw_menu{
echo " 01 ..... help"
echo " 02 ..... Cluster Members Status"
echo " 03 ..... "
echo " 04 ..... "

read RPLY'?Enter Choice ... '
case $RPLY in
00) tput clear
Dummy_Call
read BLNK'?Press Enter to continue ...'
;;
01) tput clear
command1
;;
02) tput clear
command2
;;
03) tput clear
command3
;;
04) tput clear
command4
;;
99) MNAC=1
tput clear
;;
*) echo "\nInvalid choice\n"
;;
esac
}


function command1{
your command here
}

function command2{
your command here
}

function command3{
your command here
}

function command4{
your command here
}


function Dummy_Call {
echo "Dummy Procedure Call"
}


while [ $MNAC -gt 0 ]
do
draw_menu
done
exit


--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Thanks Ygor. I thought I had tried that, but apparently I did not.

John
 
To assign default values to a variable (parameter) you can do for example this:

help=${1:-HELP} #<- Assigns value 'HELP' if param#1 is null

And use as:

if [[ &quot;$help&quot; == &quot;HELP&quot; ]]
then
...

Also, to make it easier, you may want to convert all to uppercase (or lowercase):

help=$(echo $help|tr [a-z] [A-Z])

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Also, you could use something like the following construct to accept unix type options like '-O <Value>':

set -- `getopt A:B:C:D:\? $*`

if [ $? -ne 0 ]; then
echo &quot;USAGE: MyScript &quot;
echo &quot;Options: -A <optA>&quot;
echo &quot; -B <optB>&quot;
echo &quot; -C <optC>&quot;
echo &quot; -D <optD>&quot;
echo &quot; -? Help&quot;
exit 2
fi

while [ $# -gt 0 ]; do
case $1 in
-\?) echo $HELP_MSG
exit 2
;;
-A)
OPT_A=$2
shift 2
;;
-B)
OPT_B=$2
shift 2
;;
-C)
OPT_C=$2
shift 2
;;
-D)
OPT_D=$2
shift 2
;;
--)
shift
break
;;
esac
done

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
johngiggs
Try the following:

===========================================
#!/bin/ksh
if [ &quot;$1&quot; = &quot;help&quot; -o &quot;$1&quot; = &quot;-h&quot; ]; then
echo &quot;\nHELP\n==========\n&quot;
echo &quot;Help commands&quot;
else
exit 1
fi
============================================

Hope this helps ya.
 
And yet again the [ ] needing to quote a variable where if the [[ ]] was used no quoting would be necessary.

Y'all still riding horse and buggies? [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top