標籤:
getopts 命令
用途
處理命令列參數,並校正有效選項。
文法
getopts 選項字串 名稱 [ 參數 ...]
描述
getopts 的設計目標是在迴圈中運行,每次執行迴圈,getopts 就檢查下一個命令列參數,並判斷它是否合法。即檢查參數是否以 - 開頭,後面跟一個包含在 options 中的字母。如果是,就把匹配的選項字母存在指定的變數 variable 中,並返回退出狀態0;如果 - 後面的字母沒有包含在 options 中,就在 variable 中存入一個 ?,並返回退出狀態0;如果命令列中已經沒有參數,或者下一個參數不以 - 開頭,就返回不為0的退出狀態。
shell 變數 OPTIND 中。一旦調用了shell , OPTIND 將初始化為1。當選項以 +開頭,則+ 將預先設為名稱中的值。
如果選項字串中的字元後面帶有“:”(冒號),則預期此選項將帶有參數。當選項需要選項參數時,getopts 命令就將其置於變數 OPTARG 中。
當尋找到選項字串所不包含的選項字元,或者尋找到的選項沒有所需的選項參數時:
- 如果選項字串不以:(冒號)開頭,名稱 將會被設定為 ?(問號)字元,OPTARG. 將被取消設定,並且診斷訊息將被寫入到標準錯誤中。
這種情況被認為是在將參數傳遞給所調用的應用程式的過程中所檢測到的錯誤,而不是在處理 getopts 命令的過程中所發生的錯誤;如上所述,寫入診斷訊息,但退出狀態將變為零。
- 如果選項字串以 :(冒號)開頭,名稱 將被設為 ? (問號)字元,這是對未知的選項來說的,或者為缺少的所需選項設為:(冒號)字元,OPTARG 將被設定為已尋找到的選項字元,並且 標準錯誤中將不寫入任何輸出。
樣本
- 下列 getopts 命令規定 a、b 和 c 為有效選項,並且選項 a 和 c 帶有參數:
getopts a:bc: OPT
- 下列 getopts 命令指定 a、b 以及 c 為有效選項, 並且選項 a 和 b 帶有參數,而且 getopts 在命令列遇到為定義的選項時,它將 OPT 的值設定為 ?:
getopts :a:b:c OPT
- 下列指令碼分析和顯示其參數:
aflag= bflag= while getopts ab: name do
case $name in
a) aflag=1;;
b) bflag=1 bval="$OPTARG";;
?) printf"Usage: %s: [-a] [-b value] args\n" $0 exit 2;;
esac done if [ ! -z "$aflag" ]; then printf "Option -a specified\ n" fi if [ ! -z "$bflag" ]; then printf‘Option -b "%s" specified\ n‘ "$bval" fi shift $(($OPTIND -1)) printf "Remaining arguments are: %s\n" "$*"
在Bash裡有以下用途:
optstring option 字串,會逐個匹配
varname 每次匹配成功的選項
arg 參數列表,沒寫時它會取命令列參數列表
$OPTIND 特殊變數,option index,會逐個遞增
$OPTARG 特殊變數,option argument,不同情況下有不同的值
細則1:當optstring以”:“開頭時,getopts會區分invalid option錯誤和miss option argument錯誤。
invalid option時,varname會被設成?,$OPTARG是出問題的option;
miss option argument時,varname會被設成:,$OPTARG是出問題的option。
如果optstring不以”:“開頭,invalid option錯誤和miss option argument錯誤都會使
varname被設成?,$OPTARG是出問題的option。
細則2:當optstring中的字母跟”:“時,表明該option可接參數,參數(argument)放在$OPTARG中;
如果缺參數,且optstring是以”:“開頭,則varname的值會是:,$OPTARG是該option,
否則varname的值是?,$OPTARG是該option。(參照細則1)
例子:gg.sh
[[email protected] shel]# cat gg.sh
#gg.sh
#!/bin/bash
while getopts "abc:def:ghi" flag
do
echo "$flag" $OPTIND$OPTARG # 這裡$OPTIND 是一個索引序號,$OPTARG 是選項裡所記錄的值,無值是為空白,預設情況下選項是以空格分隔
done
echo "Resetting"
OPTIND=1 while getopts "abc:def:ghi"flag
do
echo "$flag" $OPTIND $OPTARG
done
[[email protected] shel]# ./gg.sh -ab -c foo-f "foo bar" -h -gde
a 1
b 2
c 4 foo
f 6 foo bar
h 7
g 7
d 7
e 8
Resetting
a 1
b 2
c 4 foo
f 6 foo bar
h 7
g 7
d 7
e 8
上面是顯示結果。
如果調整一下所給參數的位置:
[[email protected] shel]# ./gg.sh -abcfoo -f "foo bar" -h –gde a 1
b 1
c 3 foo
f 5 foo bar
h 6
g 6
d 6
e 7
Resetting
a 1
b 1
c 3 foo
f 5 foo bar
h 6
g 6
d 6
e 7
#!/bin/bash
while getopts h:ms option
do
case "$option" in
h)
echo "option:h, value $OPTARG"
echo "next arg index:$OPTIND";;
m)
echo "option:m"
echo "next arg index:$OPTIND";;
s)
echo "option:s"
echo "next arg index:$OPTIND";;
\?)
echo "Usage: args [-h n] [-m] [-s]"
echo "-h means hours"
echo "-m means minutes"
echo "-s means seconds"
exit 1;;
esac
done
./args -h 100 –ms
option:h, value 100
next arg index:3
option:m
next arg index:3
option:s
next arg index:4
*** do something now ***
註:
1.getopts 允許把選項堆疊在一起(如 -ms)
2.如要帶參數,須在對應選項後加 :(如h後需加參數 h:ms)。此時選項和參數之間至少有一個空白字元分隔,這樣的選項不能堆疊。
3.如果在需要參數的選項之後沒有找到參數,它就在給定的變數中存入 ? ,並向標準錯誤中寫入錯誤訊息。否則將實際參數寫入特殊變數:OPTARG
4.另外一個特殊變數:OPTIND,反映下一個要處理的參數索引,初值是 1,每次執行 getopts 時都會更新。
Linux命令參數處理 shell指令碼函數getopts