Sponsored Content
Top Forums Shell Programming and Scripting option followed by : taking next option if argument missing with getopts Post 302176033 by gurukottur on Monday 17th of March 2008 07:40:30 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
getoptcvt(1)                                                       User Commands                                                      getoptcvt(1)

NAME
getoptcvt - convert to getopts to parse command options SYNOPSIS
/usr/lib/getoptcvt [-b] filename /usr/lib/getoptcvt DESCRIPTION
/usr/lib/getoptcvt reads the shell script in filename, converts it to use getopts instead of getopt, and writes the results on the standard output. getopts is a built-in Bourne shell command used to parse positional parameters and to check for valid options. See sh(1). It supports all applicable rules of the command syntax standard (see Rules 3-10, intro(1)). It should be used in place of the getopt command. (See the NOTES section below.) The syntax for the shell's built-in getopts command is: getopts optstring name [ argument...] optstring must contain the option letters the command using getopts will recognize; if a letter is followed by a colon (:), the option is expected to have an argument, or group of arguments, which must be separated from it by white space. Each time it is invoked, getopts places the next option in the shell variable name and the index of the next argument to be processed in the shell variable OPTIND. Whenever the shell or a shell script is invoked, OPTIND is initialized to 1. When an option requires an option-argument, getopts places it in the shell variable OPTARG. If an illegal option is encountered, ? will be placed in name. When the end of options is encountered, getopts exits with a non-zero exit status. The special option -- may be used to delimit the end of the options. By default, getopts parses the positional parameters. If extra arguments (argument ...) are given on the getopts command line, getopts parses them instead. So that all new commands will adhere to the command syntax standard described in intro(1), they should use getopts or getopt to parse posi- tional parameters and check for options that are valid for that command (see the NOTES section below). OPTIONS
The following option is supported: -b Makes the converted script portable to earlier releases of the UNIX system. /usr/lib/getoptcvt modifies the shell script in file- name so that when the resulting shell script is executed, it determines at run time whether to invoke getopts or getopt. EXAMPLES
Example 1: Processing the arguments for a command The following fragment of a shell program shows how one might process the arguments for a command that can take the options -a or -b, as well as the option -o, which requires an option-argument: while getopts abo: c do case $c in a | b) FLAG=$c;; o) OARG=$OPTARG;; ?) echo $USAGE exit 2;; esac done shift `expr $OPTIND - 1` Example 2: Equivalent code expressions This code accepts any of the following as equivalent: cmd -a -b -o "xxx z yy" filename cmd -a -b -o "xxx z yy" -filename cmd -ab -o xxx,z,yy filename cmd -ab -o "xxx z yy" filename cmd -o xxx,z,yy b a filename ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of getopts: LC_CTYPE, LC_MESSAGES, and NLSPATH. OPTIND This variable is used by getoptcvt as the index of the next argument to be processed. OPTARG This variable is used by getoptcvt to store the argument if an option is using arguments. EXIT STATUS
The following exit values are returned: 0 An option, specified or unspecified by optstring, was found. >0 The end of options was encountered or an error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |CSI |enabled | +-----------------------------+-----------------------------+ SEE ALSO
intro(1), getopts(1), sh(1), shell_builtins(1), getopt(3C), attributes(5) DIAGNOSTICS
getopts prints an error message on the standard error when it encounters an option letter not included in optstring. NOTES
Although the following command syntax rule (see intro(1)) relaxations are permitted under the current implementation, they should not be used because they may not be supported in future releases of the system. As in the EXAMPLES section above, -a and -b are options, and the option -o requires an option-argument. The following example violates Rule 5: options with option-arguments must not be grouped with other options: example% cmd -aboxxx filename The following example violates Rule 6: there must be white space after an option that takes an option-argument: example% cmd -ab oxxx filename Changing the value of the shell variable OPTIND or parsing different sets of arguments may lead to unexpected results. SunOS 5.10 7 Jan 2000 getoptcvt(1)
All times are GMT -4. The time now is 08:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy