Computing.Net > Forums > Unix > urgent help with getopts

urgent help with getopts

Reply to Message Icon

Original Message
Name: sujoyrc
Date: October 31, 2007 at 11:31:36 Pacific
Subject: urgent help with getopts
OS: HP-UX
CPU/Ram: 1 GB
Model/Manufacturer: HP
Comment:

A simple but urgent query -- I will be very grateful if you advise please. I am writing a script
xyz.ksh. It can be run in two modes:-
1) without any parameters.
2) with a tablename as xyz.ksh -s table_name

I am using

while getopts s:s table_name
do
case $table_name in
s) TABLE_NAME=$$OPTARG;;
done

The problem I am having is if someone is running by mistake "xyz.ksh -s" that is with the parameter but without the value I am being unable to throw an exception.

Please can you advise.


Sujoy Roychowdhury
BI Consultant


Report Offensive Message For Removal

Response Number 1
Name: lankrypt0
Date: October 31, 2007 at 12:15:56 Pacific
Subject: urgent help with getopts
Reply: (edit)

Simple IF shold fix that

if [[ $2 != "" ]];then
print $1 $2
else
print Missing an argument, stupid
fi


Report Offensive Follow Up For Removal

Response Number 2
Name: fpmurphy
Date: November 6, 2007 at 13:32:28 Pacific
Subject: urgent help with getopts
Reply: (edit)

Another way would be to check the number of command line arguments which should be either 0 or 2 as in the following ksh code snipet:

[[ $# != 0 && $# != 2 ]] && {
echo "invalid syntax"
exit 1
}

[[ $# ==2 && "$1 != "-s" ]] && {
echo "invalid command line argument: $1"
}



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: urgent help with getopts

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software