getopts : two values to a single argument ?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getopts : two values to a single argument ?
# 1  
Old 10-12-2009
getopts : two values to a single argument ?

Hi Gurus

I am trying to figure out (with not much success) how to pass two values to a single getopts argument ... for example

./script -a Tuesday sausages

The $OPTARG variable seems to only get populated with the first argument. What im looking to do is to process the first argument (i.e.make sure its a valid day of the week) . Then I want to shift along to the next argument and get the value of that to process as well. Ive looked at loads of online resources and nothing seems to point me in the right direction


while getopts "a:b:" OPT

case $OPT in
   a) echo "you have selected a"
     echo "OPTARG is set to $OPTARG"   
     # now somehow shift $OPTARG to the next argument I have provided
     echo "OPTARG is now set to $OPTARG"
   b) echo "do some different stuff"

Would anybody be able to explain to me what i would need to do to get getopts to read my second value ?

Any help would be greatly appreciated
# 2  
Old 10-12-2009

You can quote the two arguments:

 while getopts a: ARG; do
  case "$ARG" in
#    a) set -A MYARG $OPTARG; echo ${MYARG[*]}; echo ${MYARG[0]}; echo ${MYARG[1]};;
     a) MYARG=( $OPTARG ); echo ${MYARG[*]}; echo ${MYARG[0]}; echo ${MYARG[1]};;

./Test -a "arg1 arg2"
arg1 arg2

# 3  
Old 10-12-2009
Originally Posted by scottn
     a) MYARG=( $OPTARG )

The standard Unix shell does not have arrays.

while getopts a:b: opt
  case $opt in
    a) a1=$OPTARG
       eval "a2=\${$OPTIND}"
       shift 2
    b) b=$OPTARG ;;

# 4  
Old 10-12-2009

Thank you, as always, for the correction, but one could argue that there is at least two ways to define "standard"

1. That which is defined (or accepted) as such
2. That which is most commonly used (de facto standard)

The two most commonly used shells are bash and ksh. Both support arrays.

Even the sh on AIX is linked to ksh. The man page describes sh only as "the 'default' shell" - even though sh itself is a link to /usr/bin/ksh!
# 5  
Old 10-12-2009
Thank you both for the help, as far as the shell goes, i am running Solaris 8 which has a default shell of sh, although I have installed the bash packages and will probably run it in bash

thanks again for your help
# 6  
Old 10-12-2009
Originally Posted by scottn

Thank you, as always, for the correction, but one could argue that there is at least two ways to define "standard"

"The standard Unix shell" means only one thing: the shell as defined by the Single Unix Specification (which is the same as POSIX).

It is the shell which is guaranteed to be found on any Unix implementation (and those that try to be).

Bash and ksh will be found on many systems, but there's no guarantee of either being installed. That's why I don't use their features unless it gives a significant improvement in efficiency.

I especially avoid non-portable features when answering a question in a forum such as this. The answer will be read by may people, and it may be useful in a context other than that originally posed. Using bash/ksh/zsh specific features reduces the value of the post.

Last edited by cfajohnson; 10-12-2009 at 06:30 PM..
# 7  
Old 10-21-2009
The answer will be read by may people,

Please define may people.

Off topic, but funny

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue handling single quoted argument in shell script.

Below is my script that works fine and prints the desired output: #!/bin/ksh echo "$1" | while IFS= read -r dirpath do echo "DIRR_PATH:$dirpath" install_dir=$install_dir" "$dirpath done echo "Desired Output:$install_dir" Output: ./ (10 Replies)
Discussion started by: mohtashims
10 Replies

2. Programming

Python script to grep fields and use values from file 1 as argument for another command

Hi Experts, I am working one one python script in version 3.x and 2.6. Need your support to complete it Basically for both commands i have telnet to device and run command and then receiving input File 1 and File 2 I have two commands, need to grep data and output in csv file. Next script/code... (0 Replies)
Discussion started by: as7951
0 Replies

3. UNIX for Beginners Questions & Answers

Question about getopts optional argument [args...]

There are many places where I can see the syntax description for optargs, which, usually boils down to this: getopts OPTSTRING VARNAME where: OPTSTRING tells getopts which options to expect and where to expect arguments VARNAME tells getopts which shell-variable to use for option reporting... (2 Replies)
Discussion started by: sharkura
2 Replies

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

5. Shell Programming and Scripting

Passing multiple value in a single argument in Perl.

Hi all, I have below code through which trying to pick data from specific columns strating from a certain row. #!/usr/bin/perl #This script is to pick the specific fields from a files starting from a specific row # FILE -> Name of the file to be pasd at runtime. # rn -> Number of the... (4 Replies)
Discussion started by: Abhisrajput
4 Replies

6. Shell Programming and Scripting

Getopts - space in argument (OPTARG)

Hi, I want to capture space as well from the argument eg: script.ksh -m "Message from xyz" -e "" script.ksh -m 'Message from xyz' -e '' I am parsing using getopts, but for option "m" OPTARG is returning only "Message". Please use code tags next time for... (9 Replies)
Discussion started by: tostay2003
9 Replies

7. Shell Programming and Scripting

getopts ... only allow a single instance of an argument?

Hi there, if i have a simple getopts like below can i make it so that if somebody enters more than one -g argument for example, it will error with a " you cannot enter more than one -g" or something like that.? I want to only allow one instance of a -g or a -h etc .. while getopts... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

8. Shell Programming and Scripting

getopts and a list argument

Hi, I'm using bash and ksh93 compatible derivatives. In a recent getopts experience, I found myself spending far too much time on this little problem. I hope someone can help... So here's the deal. I want to build have a command line interface that accepts either zero, one, or... (4 Replies)
Discussion started by: duderonomy
4 Replies

9. Shell Programming and Scripting

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:p:f: opt do case "$opt" in -h|-\?)... (2 Replies)
Discussion started by: gurukottur
2 Replies

10. Shell Programming and Scripting

replacing single space in argument

I want to write a script which will check the arguments and if there is a single space(if 2 more more space in a row , then do not touch), replace it with _ and then gather the argument so, program will be ran ./programname hi hello hi usa now hello hello so, inside of program,... (7 Replies)
Discussion started by: convenientstore
7 Replies
Login or Register to Ask a Question