【发布时间】:2014-01-24 11:06:46
【问题描述】:
我知道如何在 Bash 中使用 getopts 处理可选参数
#!/bin/bash
while getopts ":a:p:drh" opt; do
case "$opt" in
a) echo $OPTARG;;
esac
done
但如果我的脚本需要 ./script.sh arg1 [options],我如何告诉 getopts 跳过 arg1?
$ ./script.sh -a 1234
1234
$ ./script.sh arg1 -a 1234
$ ./script.sh arg1
$ ./script.sh -a 1234 arg1
1234
正如我所见,如果将参数放在最后一个位置,我可以处理该参数。我需要在 getopts 中使用什么“正则表达式”才能让我的位置参数位于可选参数之前?
How to allow non-option arguments in any order to getopt? 的建议似乎正在重新排序参数。
【问题讨论】: