如何在bash中多次调用getopts
我有一个通用库,可以从多个解析命令行选项的脚本中使用它,但是我也希望我的个人脚本也能够处理参数...... 例如
common.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”处停止处理,因为它会在第一个“非选项”处停止
有什么方法可以解决这个问题而不需要我自己重写吗?
I have a common library that I use from several scripts that parses command line options, however I also want my individual scripts to be able to process arguments as well...
e.g.
common.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 $*
The problem si that if I call a.sh with:
a.sh -x -y foo -a -b bar
get_options stops processing at "foo" as it stops at the first "non-option"
Any way around this without rewriting things myself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需让您的通用脚本提供通用选项和处理这些选项的函数的列表,然后让其他脚本将它们可以处理的任何选项添加到选项列表中:
common.sh
a.sh
这具有以下优点:无需播放getopts 的内部管理技巧,用户可以按任意顺序指定常见和特定选项,它在一个地方处理无法识别的选项,并且还使使用功能更易于维护。
Just have your common script provide a list of common options and functions for handling those, then have the other scripts add to the option list any options which they can handle:
common.sh
a.sh
This has the following advantages: no need to play tricks with the internal houskeeping of
getopts
, the user can specify common and specific options in any order, it handles unrecognized options in a single place, and also makes the usage functions easier to maintain.我设法让它工作,不确定这是否是你想要的:
I managed to get this to work, not sure if this is what you want:
我认为您需要让您的第一个 getopts 处理器构建它未处理的命令行元素列表。此外,它还必须忽略 getopts 调用的返回值:getopts 可能会返回非零值,因为它遇到了一个不知道如何处理的命令行开关参数。所以第一个 getopts 处理器需要走到命令行的末尾。你不能相信它自己对选择何时停止、争论何时开始的判断。 (好吧,一旦你连续得到两个参数,你可能可以跳过其余的,但我将把它作为练习。)
像这样,然后:
I think you'll need to have your first getopts processor build a list of command line elements it didn't handle. Also, it will have to ignore the return value of the getopts call: getopts might return non-zero because it's hitting an argument to a command-line switch that it doesn't know how to process. So the first getopts processor needs to go to the end of the command-line. You can't trust its own judgments about when the options have stopped and the arguments have started. (Well, once you get two arguments in a row you could presumably skip the rest, but I'll leave that as an exercise.)
Something like this, then:
这里有一些很好的答案,但我认为有一个简单的解决方案:您需要增加
OPTIND
而不是将其重置为 1。即a.sh
这样您将跳过单词
foo
在参数列表中。There are some good answers here but I think there is a simple solution: you need to increment
OPTIND
instead of resetting it to 1. i.e.a.sh
In this way you will skip the word
foo
in the args list.