You can run getopts in silent mode by including a colon as the first character of the optstring. This can be used to suppress the error message.
From the getopts manpage:
If the first character of optstring is a colon, the shell variable specified
by name shall be set to the colon character and the shell variable OPTARG shall
be set to the option character found.
Thus something like the following might work for you:
#!/bin/bash
AOPT="unassigned"
BOPT="unassigned"
while getopts :ab: opt ; do
case $opt in
a) AOPT=1
;;
b) BOPT=$OPTARG
;;
:) BOPT=
;;
esac
done
echo "AOPT = $AOPT"
echo "BOPT = $BOPT"
Some examples:
rlduffy@hickory:~/test/getopts$ ./testgetopts -a -b Hello
AOPT = 1
BOPT = Hello
rlduffy@hickory:~/test/getopts$ ./testgetopts -b goodbye
AOPT = unassigned
BOPT = goodbye
rlduffy@hickory:~/test/getopts$ ./testgetopts -a -b
AOPT = 1
BOPT =