bash 腳本中,簡單點的參數選項,咱們能夠直接用位置參數 $1 $2 這樣來獲取處理了,例以下面這段代碼片斷: html
optionParam=$1 baseHdfsPath=$2 echo $optionParam|grep -qE '^(-d|-l)$' || usage echo $baseHdfsPath|grep -qE '^/' || usage if [[ $optionParam == "-l" ]] then echo --------------------$startTime---------------------- listDir "$baseHdfsPath" echo --------------------`date +'%F %T'`---------------------- else echo --------------------$startTime---------------------- downLoadFiles "$baseHdfsPath" echo --------------------`date +'%F %T'`---------------------- fi
可是若是你的參數選項不少,好比 rsync、wget 等動輒幾十上百的參數選項,那就必須用專業的工具來處理了,在 bash/shell 中咱們通常用:getopts/getopt shell
先看簡單的例子: bash
#!/bin/bash while getopts 'd:Dm:f:t:' OPT; do case $OPT in d) DEL_DAYS="$OPTARG";; D) DEL_ORIGINAL='yes';; f) DIR_FROM="$OPTARG";; m) MAILDIR_NAME="$OPTARG";; t) DIR_TO="$OPTARG";; ?) echo "Usage: `basename $0` [options] filename" esac done shift $(($OPTIND - 1))
getopts後面的字符串就是可使用的選項列表,每一個字母表明一個選項,後面帶:的意味着選項除了定義自己以外,還會帶上一個參數做爲選項的值,好比d:在實際的使用中就會對應-d 30,選項的值就是30;getopts字符串中沒有跟隨:的是開關型選項,不須要再指定值,至關於true/false,只要帶了這個參數就是true。若是命令行中包含了沒有在getopts列表中的選項,會有警告信息,若是在整個getopts字符串前面也加上個:,就能消除警告信息了。 工具
使用getopts識別出各個選項以後,就能夠配合case來進行相應的操做了。操做中有兩個相對固定的「常量」,一個是OPTARG,用來取當前選項的值,另一個是OPTIND,表明當前選項在參數列表中的位移。注意case中的最後一個選擇──?,表明這若是出現了不認識的選項,所進行的操做。 使用getopts處理參數雖然是方便,但仍然有兩個小小的侷限:
1.選項參數的格式必須是-d val,而不能是中間沒有空格的-dval。
2.全部選項參數必須寫在其它參數的前面,由於getopts是從命令行前面開始處理,遇到非-開頭的參數,或者選項參數結束標記--就停止了,若是中間遇到非選項的命令行參數,後面的選項參數就都取不到了。
3.不支持長選項, 也就是--debug之類的選項
再看個實例: ui
#!/bin/bash echo 初始 OPTIND: $OPTIND while getopts "a:b:c" arg #選項後面的冒號表示該選項須要參數 do case $arg in a) echo "a's arg:$OPTARG" #參數存在$OPTARG中 ;; b) echo "b's arg:$OPTARG" ;; c) echo "c's arg:$OPTARG" ;; ?) #當有不認識的選項的時候arg爲? echo "unkonw argument" exit 1 ;; esac done echo 處理完參數後的 OPTIND:$OPTIND echo 移除已處理參數個數:$((OPTIND-1)) shift $((OPTIND-1)) echo 參數索引位置:$OPTIND echo 準備處理餘下的參數: echo "Other Params: $@"結果:
june@Win7 192.168.1.111 02:32:45 ~ > bash b.sh -a 1 -b 2 -c 3 test -oo xx -test 初始 OPTIND: 1 a's arg:1 b's arg:2 c's arg: 處理完參數後的 OPTIND:6 移除已處理參數個數:5 參數索引位置:6 準備處理餘下的參數: Other Params: 3 test -oo xx -test june@Win7 192.168.1.111 02:32:49 ~ > bash b.sh -a 1 -c 3 -b 2 test -oo xx -test # 非參數選項注意順序與值,不要多傳 初始 OPTIND: 1 a's arg:1 c's arg: 處理完參數後的 OPTIND:4 移除已處理參數個數:3 參數索引位置:4 準備處理餘下的參數: Other Params: 3 -b 2 test -oo xx -test june@Win7 192.168.1.111 02:33:14 ~ > bash b.sh -a 1 -c -b 2 test -oo xx -test 初始 OPTIND: 1 a's arg:1 c's arg: b's arg:2 處理完參數後的 OPTIND:6 移除已處理參數個數:5 參數索引位置:6 準備處理餘下的參數: Other Params: test -oo xx -test june@Win7 192.168.1.111 02:33:22 ~ >
先來看下getopt/getopts的區別
1. getopts是bash內建命令的, 而getopt是外部命令
2. getopts不支持長選項, 好比: --date
3. 在使用getopt的時候, 每處理完一個位置參數後都須要本身shift來跳到下一個位置, getopts只須要在最後使用shift $(($OPTIND - 1))來跳到parameter的位置。
4. 使用getopt時, 在命令行輸入的位置參數是什麼, 在getopt中須要保持原樣, 好比 -t , 在getopt的case語句中也要使用-t, 而getopts中不要前面的-。
5. getopt每每須要跟set配合使用
6. getopt -o的選項注意一下 spa
7. getopts 使用語法簡單,getopt 使用語法較複雜 .net
8. getopts 不會重排全部參數的順序,getopt 會重排參數順序 命令行
9. getopts 出現的目的是爲了代替 getopt 較快捷的執行參數分析工做 debug
下面是getopt自帶的一個例子: unix
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> #!/bin/bash # A small example program for using the new getopt(1) program. # This program will only work with bash(1) # An similar program using the tcsh(1) script language can be found # as parse.tcsh # Example input and output (from the bash prompt): # ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long " # Option a # Option c, no argument # Option c, argument `more' # Option b, argument ` very long ' # Remaining arguments: # --> `par1' # --> `another arg' # --> `wow!*\?' # Note that we use `"$@"' to let each command-line parameter expand to a # separate word. The quotes around `$@' are essential! # We need TEMP as the `eval set --' would nuke the return value of getopt. #-o表示短選項,兩個冒號表示該選項有一個可選參數,可選參數必須緊貼選項 # 如-carg 而不能是-c arg #--long表示長選項 #"$@" :參數自己的列表,也不包括命令自己 # -n:出錯時的信息 # -- :舉一個例子比較好理解: #咱們要建立一個名字爲 "-f"的目錄你會怎麼辦? # mkdir -f #不成功,由於-f會被mkdir看成選項來解析,這時就可使用 # mkdir -- -f 這樣-f就不會被做爲選項。 TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \ -n 'example.bash' -- "$@"` if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi # Note the quotes around `$TEMP': they are essential! #set 會從新排列參數的順序,也就是改變$1,$2...$n的值,這些值在getopt中從新排列過了 eval set -- "$TEMP" #通過getopt的處理,下面處理具體選項。 while true ; do case "$1" in -a|--a-long) echo "Option a" ; shift ;; -b|--b-long) echo "Option b, argument \`$2'" ; shift 2 ;; -c|--c-long) # c has an optional argument. As we are in quoted mode, # an empty parameter will be generated if its optional # argument is not found. case "$2" in "") echo "Option c, no argument"; shift 2 ;; *) echo "Option c, argument \`$2'" ; shift 2 ;; esac ;; --) shift ; break ;; *) echo "Internal error!" ; exit 1 ;; esac done echo "Remaining arguments:" for arg do echo '--> '"\`$arg'" ; done關於 getopt 選項重排的解釋:
好比咱們使用
./test -a -b arg arg1 -c
你能夠看到,命令行中多了個arg1參數,在通過getopt和set以後,命令行會變爲:
-a -b arg -c -- arg1
$1指向-a,$2指向-b,$3指向arg,$4指向-c,$5指向--,而多出的arg1則被放到了最後。
getopt 對參數順序進行重排的意義:這樣能夠將帶 "-" 或 "–" 的參數寫在其餘參數的前面,也能夠寫在後面,而 getopts 是沒有這樣的能力的,具體沒有的緣由就是由於 getopts 直接進入了 while 循環處理參數,而 getopt 遊一個 set — ${ARGS} 的過程。另外還要注意到的是,在使用 getopt 處理完參數以後,"${@}" 變量 「被清洗乾淨了」 ,裏面包含了全部不帶 "-" 或 "–" 的參數,因此你能夠繼續使用 ${1},${2} 等來調用他們。
一、Bash Shell中命令行選項/參數處理
http://www.cnblogs.com/FrankTan/archive/2010/03/01/1634516.html
二、bash處理命令行參數:getopts/getopt
http://blog.chinaunix.net/uid-21651880-id-3392466.html
三、getopt 使用教程並與 getopts 比較
http://hiaero.net/getopts-versus-getopt/
四、bash getopts, short options only, all require values, own validation