getopts takes options for parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts takes options for parameters
# 1  
Old 02-17-2006
getopts takes options for parameters

Here is my post with a question about getopts. I am running korn shell on Solaris 5.8. I am trying to ensure that certain options require a parameter, which is easy enough. I have found that if multiple options are entered on the command line together, but the parameter for one of the options is missing, getopts will simply take the next option letter as the parameter. Example:

#!/bin/ksh

USAGE=$([-a] [-b file_b] [-c] [-d] [-e file_e] [-f]);
while getopts "ab:cde:f" opt
do
case $opt in
a) minusa=$opt;;
b) minusb=$opt
file_b=$OPTARG;;
c) minusc=$opt;;
d) minusd=$opt;;
e) minuse=$opt
file_e=$OPTARG;;
f) minusf=$opt''
/?) echo Unrecognized parameter
exit 1;;
esac

If I run the example script with any one of the options which require a parameter, or if the option with the required parameter is the last in the list, getopts will tell me that the option requires a parameter.

$example.shl -b or
$example.shl -ab
Both will return an error to standard output stating the option requires a parameter.

But, if I enter the option in the following manner, it will not tell me when the parameter is required, it will take the next option as the parameter:
$example.shl -abc or
$example.shl -a -b -c

How can I force getopts to recognize the difference between an option and a parameter?

Thanks,

Randy
# 2  
Old 02-17-2006
I add another case statement for these kinds of parameters:
Code:
#!/bin/ksh

USAGE=$([-a] [-b file_b] [-c] [-d] [-e file_e] [-f]);
while getopts "ab:cde:f" opt
do
    case ${opt} in
        b|e)
            [[ ${OPTARG} = -* ]]   && usage "Invalid parameter \"${OPTARG}\" provided for agurment \"-${opt}!\""
            [[ ${#OPTARG} -eq 0 ]] && usage "Argument \"-${opt}\" requires a parameter!${OPTARG}"
        ;;
    esac

    case $opt in
        a) minusa=$opt;;
        b) minusb=$opt
        file_b=$OPTARG;;
        c) minusc=$opt;;
        d) minusd=$opt;;
        e) minuse=$opt
        file_e=$OPTARG;;
        f) minusf=$opt''
        /?) echo Unrecognized parameter
        exit 1;;
    esac

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getopts with optional parameters

Hi Unix Gurus, i am on learning path of unix, and yet to discover many things. I came across with this requirement where i need to pass parameters but the position of parameters is not fixed so after doing some google search got to know "getopts" can handle that. So here is my code: function... (3 Replies)
Discussion started by: gnnsprapa
3 Replies

2. Shell Programming and Scripting

Using getopts for handling multiple options

Hi Guys, I have created a script for our automated DB creation, it works fine with default option(-d). $ ./test_db.ksh -d abc 11 dev -d is Default option ORACLE_SID=abc ORACLE_VERSION=11 ENV_TYPE=dev For creating a customized DB, i thought of giving the user different options.... (8 Replies)
Discussion started by: veeresh_15
8 Replies

3. Shell Programming and Scripting

ksh processing options and parameters

I have a requirement where I need to process both options and parameters. command line call ie xxx.ksh -sid TEST1 -search_str LOCKED user1 user2 ..... I am using the following peice of code but I am usure how I can loop through all my parameters user1, user2, ... Note at the minium... (2 Replies)
Discussion started by: BeefStu
2 Replies

4. Shell Programming and Scripting

Order of getopts parameters

Hi, Does the order of argument and non-argument command line parameters matter to getopts? with a getopts line in my script of getopts p:cs opt a command line of <script> -p 5 -s only picks up the -p option, while <script> -s -p 5 picks up both. Removing the space between the p and the... (3 Replies)
Discussion started by: rojomoke
3 Replies

5. Shell Programming and Scripting

Intersperse arguments and options w/ getopts

Is it possible to get a script that uses getopts to accept options and arguments in any order? eg. -g -h 2 4 works like -g 2 -h 4. (1 Reply)
Discussion started by: lee.n.doan
1 Replies

6. Programming

Parse parameters with getopts

Hi, this is my problem I have script with two parameters -n name and -s surname. Both have arguments and I want to know how parse these parameters with getopts. When you write ./names -n John -s White it find you all persons, which name is John White, but when you write ./names -n John ... (1 Reply)
Discussion started by: frees
1 Replies

7. AIX

tuning network parameters : parameters not persist after reboot

Hello, On Aix 5.2, we changed the parameters tcp_keepinit, tcp_keepintvl and tcp_keepidle with the no command. tunrestore -R is present in inittab in the directory /etc/tunables we can clearly see the inclusion of parameters during reboot, including the file lastboot.log ... (0 Replies)
Discussion started by: dantares
0 Replies

8. Shell Programming and Scripting

Get Options and Parameters

Hi there. Could someone explain how do i get the options separated from the arguments. My problem is that i have a lot of options and in all of those options i need to have the parameters available to use in that option and all the others. How can i take the arguments before i cycle throw the... (4 Replies)
Discussion started by: KitFisto
4 Replies
Login or Register to Ask a Question