我知道如何在 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 中的建议?似乎正在重新排列论点。