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

#!/bin/bash

while getopts "a:b:" OPT
do

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"
   ;;
   esac
done

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

Hi.

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]};;
  esac
done


./Test -a "arg1 arg2"
arg1 arg2
arg1
arg2

The standard Unix shell does not have arrays.

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

Hi.

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!

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

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

Please define may people.

Off topic, but funny

Jaysunn