On Standards-Compliance
What you are trying is not supposed to work in the first place.
POSIX utility syntax guideline #5 states:
One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when grouped behind one '-' delimiter.
So, the option taking an option-argument (-w
in this case) is only allowed to be the last one in a group started by a single -
.
On Making It Work Anyhow
If you can't deal with standard-compliant behavior, you can't use getopts
, so you need to write your own logic. One way to do that might look like the following:
#!/usr/bin/env bash
# ^^^^- note bash, not sh; the below code uses non-POSIX extensions
while (( $# )) && [[ $1 = -* ]]; do
arg=${1#-}; shift
while [[ $arg ]]; do
case $arg in
l*) flag_l=1; arg=${arg#l};;
u*) flag_u=1; arg=${arg#u};;
w*)
flag_w=1
rest=${arg#w}
if [[ -z $rest ]]; then
arg=$1; shift; rest=$arg
fi
if [[ $rest =~ ^([[:digit:]]+)(.*) ]]; then
w_value=${BASH_REMATCH[1]}
arg=${BASH_REMATCH[2]}
else
echo "ERROR: -w not followed with a number" >&2
exit 1
fi
;;
*) echo "Unrecognized flag: $arg" >&2; exit 1;;
esac
done
done
echo "After parsing:"
echo "flag_w = ${flag_w:-0}"
echo "flag_l = ${flag_l:-0}"
echo "flag_u = ${flag_u:-0}"
echo "w_value = ${w_value:-0}"
See this running in the online interpreter at https://ideone.com/eDrlHd