The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
g++ and the -R option eternalflame High Level Programming 0 04-14-2008 01:57 PM
-n option ravi raj kumar Shell Programming and Scripting 1 01-03-2008 09:20 AM
ps: 65535 is an invalid non-numeric argument for -p option gogogo SUN Solaris 5 11-19-2006 06:20 PM
su option lesstjm UNIX for Advanced & Expert Users 1 11-02-2005 01:54 PM
cut -f option 435 Gavea UNIX for Dummies Questions & Answers 1 11-10-2003 05:50 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-17-2008
gurukottur gurukottur is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 40
option followed by : taking next option if argument missing with getopts

Hi all,

I am parsing command line options using getopts.

The problem is that mandatory argument options following ":" is taking next option as argument if it is not followed by any argument.
Below is the script:

while getopts :hd:t:s:l:f: opt
do
case "$opt" in
-h|-\?) usage;;
-d) DEBUG=true;export SCRIPT_LOG_LEVEL=DEBUG ;;
-t) DEVTYPE="$OPTARG" ;;

-s) SIG_IP="$OPTARG"; export SOCKS5_SERVER="$OPTARG:9001" ;;
-l) HOST_LOGIN="$OPTARG";;

-p) PASSWORD="$OPTARG" ;;

-f) cl_cfile_specd=1
export RUNNING_CONFIG_FILE="$OPTARG" ;;

*) logmsg "Unrecognized param usage";;
esac
done
if I run the script as:
$./script.sh -d -t rdsk -s 12.3.4.5 -l guru -p -f /usr/local/sc.conf
the option -p is taking -f as argument.

Please help me how to throw an error and exit the script if mandatory argument is not passed to the script.

Also how to deal with options having -- ie --logfile.

Please help with the above problem
  #2 (permalink)  
Old 03-17-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 699
Hi.

How should getopts know that the string "-f" is not an appropriate value for the argument of option "-p"? ... cheers, drl

PS In the getopts I use, the case selectors should not have leading "-" characters.

PPS Also note that you have set "d:", but did not use OPTARG in the case selector for "-d".

Last edited by drl; 03-17-2008 at 12:36 PM..
  #3 (permalink)  
Old 03-17-2008
era
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
As a remark on design, perhaps they should not be "options" if they are mandatory.

After the while loop, check if password or config file is unset, and die if it is?

I don't think there's a standard way to get long options with getopts; you can roll your own, though.

Code:
while :
do
  case $# in 0) break;; esac
  case $1 in 
    -h|-\?|--help) usage;;
    -d|--debug) DEBUG=true;export SCRIPT_LOG_LEVEL=DEBUG; shift ;;
    -t|--type) DEVTYPE="$2"; shift; shift ;;
    -s|--socks) SIG_IP="$2"; export SOCKS5_SERVER="$2:9001"; shift; shift ;;
    -l|--login) HOST_LOGIN="$2"; shift; shift;;
    -p|--password) PASSWORD="$2"; shift; shift ;;
    -f|--config-file) cl_cfile_specd=1
        export RUNNING_CONFIG_FILE="$2"; shift; shift ;;
    -*) logmsg "Unrecognized param usage";;
  esac
done
Making it die gracefully when an option which requires an argument doesn't receive any (i.e. $2 is not set at all) is left as an exercise. The problem of seeing that $2 is the next option is artificial intelligence (or you could invent a separate mechanism for specifying an argument which begins with dash, so that you can forbid option arguments to start with a dash in the general case. I don't know if that's good or bad usability).
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0