我有一个公共(public)库,我从几个解析命令行选项的脚本中使用它,但是我也希望我的各个脚本也能够处理参数... 例如
通用.sh:
function get_options {
echo -e "in getoptions"
echo $OPTIND
while getopts ":ab:" optionName; do
[ ... processing code ... ]
done
}
a.sh
. ./common.sh
function get_local_options {
echo -e "in getoptions"
echo $OPTIND
while getopts ":xy:" optionName; do
[ ... processing code ... ]
done
}
get_local_options $*
OPTIND=1
get_options $*
如果我调用 a.sh 的问题是:
a.sh -x -y foo -a -b bar
get_options 在“foo”处停止处理,因为它在第一个“非选项”处停止
在不自己重写东西的情况下有什么办法解决这个问题吗?
最佳答案
foo() {
unset OPTIND
while getopts ...
do
done
}
关于bash - 如何在 bash 中多次调用 getopts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2189281/