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