【发布时间】:2013-04-17 16:10:07
【问题描述】:
我正在尝试在 bash 中使用 getopts 处理命令行参数。其中一项要求是处理任意数量的选项参数(不使用引号)。
第一个示例(仅获取第一个参数)
madcap:~/projects$ ./getoptz.sh -s a b c
-s was triggered
Argument: a
第二个例子(我希望它表现得像这样,但不需要引用参数”
madcap:~/projects$ ./getoptz.sh -s "a b c"
-s was triggered
Argument: a b c
有没有办法做到这一点?
这是我现在拥有的代码:
#!/bin/bash
while getopts ":s:" opt; do
case $opt in
s) echo "-s was triggered" >&2
args="$OPTARG"
echo "Argument: $args"
;;
\?) echo "Invalid option: -$OPTARG" >&2
;;
:) echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
【问题讨论】:
-
这可能会有所帮助:*.com/a/7530327/1983854
-
需要更多细节。当给定
getoptz.sh -s a -b c时,你想要什么行为?-b是-s的参数,还是-表示新选项?
标签: linux bash shell unix getopts