This is simple, but the documentation is only in the getopt()
man page from Solaris and in the getopts
documentation from bosh
:
http://schilytools.sourceforge.net/man/man1/bosh.1.html
scroll down to page 47 in the version from December 2019.
Let me edit your example:
while getopts ":t:(test)c:(call)" name; do
case $name in
t) first=$OPTARG;;
c) second=$OPTARG;;
:) echo "Missing argument for -$OPTARG"; break;;
*) echo 'Invalid option'; break;;
esac
done
shift $((OPTIND-1))
Note that you may need to quote arguments that contain parenthesis....
This accepts -c
and --call
.
Note that this only works with
- bosh, the maintained POSIX enhanced Bourne Shell from schilytools on any OS
- sh , the old Bourne Shell on Solaris 10
- ksh93 on any OS
bosh
in addition also supports UNIX style long options -long
if optstring
starts with ()
.
Both, ksh93
and bosh
in addition support options that start with +
if optstring
starts with a +
. In such a case, $name
contains +c
instead of c
, if the program has been called with +c
.
BTW: Only bosh
supports a method to support long options that have no adjacent short option. See bosh
man page.
I am the author of schilytools.
getopt
is outdated since ~1988 and all solutions based on the GNU vendor specific variant are non-portable. – schily Dec 12 '19 at 13:26