There are many places where I can see the syntax description for optargs, which, usually boils down to this:
getopts OPTSTRING VARNAME [ARGS...]
where:
OPTSTRING tells getopts which options to expect and where to expect arguments
VARNAME tells getopts which shell-variable to use for option reporting
ARGS tells getopts to parse these optional words instead of the positional parameters
None of the examples I've found show how to use the optional ARGS... variable length argument list. All examples focus on the positional arguments, which I know how to use.
I've tried several tests, but, the only effect of adding ARGS... to my script causes the positional arguments to be ignored ... nothing I've tried does anything with the optional arguments.
So, I'm curious ... can anyone give me a short example of how a variable length argument lists specified by ARGS... is used?
Thanks.
Also, please don't ask me to provide everything I've tried. They all fail, and, I'd rather cut to the chase with one simple example of what works.
Strange - appending a "line" of options should (and DOES!) make getopts evaluate those in lieu of the positional parameters, and it does for me. Pls (I know you dislike this) show an example of your failed attempts.
EDIT: Are you sure you did differentiate between the bash builtin getopts and the external command getopt ?
Here are the guts of a test script, "s0", which calls helper scripts "s1" and "s2", using getopt and getopts , adapted from A small example on Bash getopts. #bash #getopt #getopts . GitHub, which in turn, was one of the top results of a Google search for keywords short example of getopt and getopts:
# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
pl " Results, getopt, s1 -a mystuff x y z:"
./s1 -a mystuff x y z
pl " Results, getopts, s2 -a mystuff x y z:"
./s2 -a mystuff x y z
pl " Code for s1 and s2:"
pe
head -50 s1 s2
producing:
$ ./s0
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution : Debian 8.11 (jessie)
bash GNU bash 4.3.30
-----
Results, getopt, s1 -a mystuff x y z:
Got option 'a' with argument 'mystuff'
Remaining args are: <'x' 'y' 'z'>
-----
Results, getopts, s2 -a mystuff x y z:
Got option 'a' with argument mystuff
Remaining args are: <x y z>
-----
Code for s1 and s2:
==> s1 <==
#!/bin/bash
#
# Example using getopt (vs builtin getopts) that can also handle long options.
# Another clean example can be found at:
# http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt
#
aflag=n
bflag=n
cargument=
# Parse options. Note that options may be followed by one colon to indicate
# they have a required argument
if ! options=$(getopt -o a:bc: -l along,blong,clong: -- "$@")
then
# Error, getopt will put out a message for us
exit 1
fi
set -- $options
while [ $# -gt 0 ]
do
# Consume next (1st) argument
case $1 in
-b|--blong)
bflag="y" ;;
# Options with required arguments, an additional shift is required
-a|--along)
aargument="$2" ; shift
echo "Got option 'a' with argument ${aargument}"
aflag="y" ;;
-c|--clong)
cargument="$2" ; shift;;
(--)
shift; break;;
(-*)
echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
(*)
break;;
esac
# Fetch next argument as 1st
shift
done
shift $((OPTIND-1))
echo "Remaining args are: <${@}>"
==> s2 <==
#!/bin/bash
while getopts ":da:" opt; do
case $opt in
d)
echo "Entering DEBUG mode"
;;
a)
echo "Got option 'a' with argument ${OPTARG}"
;;
:)
echo "Error: option ${OPTARG} requires an argument"
;;
?)
echo "Invalid option: ${OPTARG}"
;;
esac
done
shift $((OPTIND-1))
echo "Remaining args are: <${@}>"