bash输出无效选项

Ben*_*min 1 linux bash optional-parameters switch-statement

假设我有以下脚本来处理选项:

while getopts dsf opts 2>/dev/null
do
    case $opts in
    d) echo "d";;
    s) echo "s";;
    \?) echo "Error: An invalid option [?] was entered.";
        exit 1;;
    esac
done
Run Code Online (Sandbox Code Playgroud)

我想用我输入的无效开关替换[?].所以,如果我进入

./myscript -z //output: Error: An invalid option [-z] was entered.
Run Code Online (Sandbox Code Playgroud)

我怎么能抓到那个无效的开关?使用我的$ opts变量会显示一个问号.

Ign*_*ams 5

来自help getopts:

getopts reports errors in one of two ways.  If the first character
of OPTSTRING is a colon, getopts uses silent error reporting.  In
this mode, no error messages are printed.  If an invalid option is
seen, getopts places the option character found into OPTARG.
Run Code Online (Sandbox Code Playgroud)