根据要求,此方法避免解析整个命令行。--
只为 收集高达 的参数b.sh
。然后 b 的参数被剥离,只有剩余的参数被传递给a.sh
.
b.sh
用 调用b.sh -b b1option -B b2option -- -a1 a1option -a2 a2option
。在这一行中,双破折号--
表示选项的结束b.sh
。下面解析--
for use by之前的选项b.sh
,然后从 the 中删除 b 参数,$@
这样您就可以将其传递给,a.sh
而不必担心a.sh
可能会给您带来什么错误。
while getopts ":b:B:" opt; do
case $opt in
b) B1=${OPTARG}
;;
B) B2=${OPTARG}
;;
esac
done
## strips off the b options (which must be placed before the --)
shift $(({OPTIND}-1))
a.sh "$@"
注意:此方法使用 bash 内置 getopts。Getopts(相对于 getopt,没有 s)只接受单字符选项;因此,我使用b
andB
而不是b1
and b2
。
我最喜欢的getopts参考。