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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

urgent help with getopts

Reply to Message Icon

Name: sujoyrc
Date: October 31, 2007 at 11:31:36 Pacific
OS: HP-UX
CPU/Ram: 1 GB
Product: 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



Sponsored Link
Ads by Google

Response Number 1
Name: lankrypt0
Date: October 31, 2007 at 12:15:56 Pacific
Reply:

Simple IF shold fix that

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


0

Response Number 2
Name: fpmurphy
Date: November 6, 2007 at 13:32:28 Pacific
Reply:

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"
}



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: urgent help with getopts

Urgent help with KSH script www.computing.net/answers/unix/urgent-help-with-ksh-script/8014.html

Help with gcc www.computing.net/answers/unix/help-with-gcc/5280.html

Help with Bourne Shellscript (More) www.computing.net/answers/unix/help-with-bourne-shellscript-more/5005.html