
07-26-2008
|
|
Shell programmer, author
|
|
|
Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
|
|
Quote:
Originally Posted by kdelok
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?
|
Save and remove the leading arguments before calling getopts, e.g.:
Code:
n=1
while [ $# -gt 0 ]
do
case $1 in
-*) break;;
*) eval "arg_$n=\$1"; n=$(( $n + 1 )) ;;
esac
shift
done
while getopts abc opt
do
case $opt in
a|b|c) echo opt $opt ;;
esac
done
|