我想在bash中使用getopts處理命令行參數。其中一個要求是處理任意數量的選項參數(不使用引號)。使用getopts(bash)的多選項參數
1例(只抓住第一個參數)
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
這可能會幫助:http://stackoverflow.com/a/7530327/1983854 – fedorqui 2013-04-24 08:34:12
詳細信息是必要的。當給定'getoptz.sh -s a -b c'時,你想要什麼行爲? '-b'是'-s'的參數,還是'-'表示一個新選項? – 2013-04-24 11:26:43
相關,但決不是重複,[調用不同的選項和不同的參數爲每個選項](http://stackoverflow.com/questions/15442950/)。一般來說,最好使用標準的命令接口指南[POSIX Utility Conventions](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html) – 2013-04-24 13:17:14