Visit Our UNIX and Linux User Community


option followed by : taking next option if argument missing with getopts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting option followed by : taking next option if argument missing with getopts
# 1  
Old 03-17-2008
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:lSmilie: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  
Old 03-17-2008
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 02:36 PM..
# 3  
Old 03-17-2008
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).
era

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Missing argument for option: n Error

I am trying to execute the cli.sh script in another shell script passing arguments and getting the below error. Myscript.sh #!/bin/sh /home/runAJobCli/cli.sh runAJobCli -n $Taskname -t $Tasktype I am passing the below 2 arguments and it giving error ./Myscript.sh T_SAMPLE_TEST MTT... (11 Replies)
Discussion started by: Info_Geek
11 Replies

2. Shell Programming and Scripting

ksh - default value for getopts option's argument

Hello everyone, I need help in understanding the default value for getopts option's argument in ksh. I've written a short test script: #!/bin/ksh usage(){ printf "Usage: -v and -m are mandatory\n\n" } while getopts ":v#m:" opt; do case $opt in v) version="$OPTARG";; ... (1 Reply)
Discussion started by: da1
1 Replies

3. Shell Programming and Scripting

Getopts option -please help

Hello, I am using below code in AIX env to interpret -n option given in argument while executing the script .I want to give another argument -t #!/bin/sh #set -x while getopts ":n:" opt; do case "$opt" in n) host=$OPTARG shift 2 ;; *)... (3 Replies)
Discussion started by: Vishal_dba
3 Replies

4. Shell Programming and Scripting

passing an option as an argument!

Hi Folks I have got to the point where I can specify the arguments but how to pass an option is still mystery to me. Example: temp.csh a b c d set temp1 = $argv set temp2 = $argv set temp3 = $argv echo $temp1 a echo $temp2 b echo $temp3 c d I WANT: temp.csh a b c d -S 1 set temp1... (2 Replies)
Discussion started by: dixits
2 Replies

5. Boot Loaders

PXE boot not taking the init= option

Hi Experts, I am doing PXE boot for my GNU/Linux device and pxelinux.0 loads the kernel as well as initrd images I have mentioned in the config file but it looks like it is not considering the init= option. Instead it starts the default INIT program. I wanted my customized init program to be... (3 Replies)
Discussion started by: learn more
3 Replies

6. Shell Programming and Scripting

How to execute default in getopts when no option is given ?

hi, here is a ksh script i wrote using getopts... i want to find out how i can run it in default mode when no option is mentioned and no arguments are provided... ? i.e if the script name is final1, then just running final1 should run in default mode.... while getopts 1:2:3:4: mode ... (1 Reply)
Discussion started by: pravsripad
1 Replies

7. Shell Programming and Scripting

getopts fails to error on option w/o dash

I have a script with several options and during testing I found that the \? option does not handle options without dashes as I would expect. Then I run the script with any option that does not include a dash, it runs the script when I would expect \? to catch it and error. I've tried this with... (2 Replies)
Discussion started by: HexKnot
2 Replies

8. Shell Programming and Scripting

getopts with repeat of same option

Hello, Does getopts have some way of handling the use of an option that requires a parameter more than once on the command line. e.g. mycmd -a john -a jane I came up with a solution using arrays (shown below), but wonder if getopts has some other way of handling it. Other solutions... (2 Replies)
Discussion started by: CarlosNC
2 Replies

9. Shell Programming and Scripting

getopts: bad option(s)

Hi, I have a script that ran perfectly on Solaris 5.8 However after upgrade to Solaris 5.10 it started failing. I invoke the script as below: ./TestScript3.ksh --dir $APP_DATA_IN_OLD $NDM_DATA/$NEXT_FILE When i execute it i get the following error "getopts: dir bad option(s)". Please let... (1 Reply)
Discussion started by: JoeJoseph
1 Replies

10. Shell Programming and Scripting

getopts with non-option arguments?

Hello everyone, Is it possible to use getopts and also receive arguments without option flags? e.g. myscript arg1 arg2 -a arg3 -b arg4 If so, how do you stop getopts from exiting as soon as it detects the non-option arguments? (2 Replies)
Discussion started by: kdelok
2 Replies

Featured Tech Videos