I have the following bash function that uses getopts
to parse positional arguments. Am trying to make options fail and get to print error messages.
pfa ()
{
local OPTIND OPTARG
local shortopts=":Vuhv:f:sc:"
while getopts $shortopts arg; do
case $arg in
("f") fvar="$OPTARG" ;;
("c") cnum="$OPTARG" ;;
("s") sort=1 ;;
(":")
printf '%s\n' ": Argument not supplied to -${OPTARG}"
break
;;
("?")
printf '%s\n' "? Option not recognised by shortopts"
break
;;
(*)
printf '%s\n' "Invoke \`pfa -h' for details."
;;
esac
done
}
I am running the two commands below, one with an unrecognised -g
and another with a missing argument value to -g
. The problem is that when using -f
, it is not tollingg me that the argument value has not been supplied.
tolu@flora: ~
pfa -g -c 200
? Option not recognised by shortopts
tolu@flora: ~
pfa -f -c 200
tolu@flora: ~
pfa -c 200 -f
: Argument not supplied to -f