假设我正在getopts
使用选项字符串 ":a:b" 在 Bash 脚本中运行,并且我提供了以下命令行参数:
./script.sh -a foo bar -b bar zappo
“foo”的实例是该选项的预期参数,a
并且该选项b
没有预期参数。但是,“bar”的实例和“zappo”的实例都是不可接受的getopts
。运行后getopts
,如何回显包含所有不可接受参数列表的变量?我怎样才能生成列表“bar bar zappo”?谢谢!
为了您的方便,这里有一些代码:
#!/bin.bash
option_string=":a:b"
unexpected_parameters=""
OPTIND=1
while getopts "${option_string}" options; do
case ${options} in
a)
echo "-a triggered with parameter ${OPTARG}" >&2 # output to STDERR
;;
b)
echo "-b triggered" >&2 # output to STDERR
;;
\?)
echo "invalid option: -${OPTARG}" >&2 # output to STDERR
;;
esac
#<insert code magic... unexpected_parameters="${unexpected_parameters} "$(MAGIC)"">
done
echo "unexpected parameters: ${unexpected_parameters}"