歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> getopt/getopts:Bash中命令行選項/參數處理

getopt/getopts:Bash中命令行選項/參數處理

日期:2017/3/1 14:43:23   编辑:關於Linux
getopt/getopts:Bash中命令行選項/參數處理 0.引言 寫程序的時候經常要處理命令行參數,本文描述在Bash下的命令行處理方式。 選項與參數: 如下一個命令行: [vb] ./test.sh -f config.conf -v --prefix=/home 我們稱-f為選項,它需要一個參數,即config.conf, -v 也是一個選項,但它不需要參數。 --prefix我們稱之為一個長選項,即選項本身多於一個字符,它也需要一個參數,用等號連接,當然等號不是必須的,/home可以直接寫在--prefix後面,即--prefix/home,更多的限制後面具體會講到。 在bash中,可以用以下三種方式來處理命令行參數,每種方式都有自己的應用場景。 手工處理方式 getopts getopt 下面我們依次討論這三種處理方式。 1. 手工處理方式 在手工處理方式中,首先要知道幾個變量,還是以上面的命令行為例: $0 : ./test.sh,即命令本身,相當於C/C++中的argv[0] $1 : -f,第一個參數. $2 : config.conf $3, $4 ... :類推。 $# 參數的個數,不包括命令本身,上例中$#為4. $@ :參數本身的列表,也不包括命令本身,如上例為 -f config.conf -v --prefix=/home $* :和$@相同,但"$*" 和 "$@"(加引號)並不同,"$*"將所有的參數解釋成一個字符串,而"$@"是一個參數數組。如下例所示: [vb] #!/bin/bash for arg in "$*" do echo $arg done for arg in "$@" do echo $arg done 執行./test.sh -f config.conf -n 10 會打印: -f config.conf -n 10 #這是"$*"的輸出 #以下為$@的輸出 -f config.conf -n 10 所以,手工處理的方式即對這些變量的處理。因為手工處理高度依賴於你在命令行上所傳參數的位置,所以一般都只用來處理較簡單的參數。如 “./test.sh 10”,而很少使用“./test -n 10”這種帶選項的方式。 典型用法為: [vb] #!/bin/bash #if [ -n "$1" ],參數不為空 if [ "$1" != "" ] then #...有參數 else then #...沒有參數 fi 手工處理方式能滿足大多數的簡單需求,配合shift使用也能構造出強大的功能,但在要處理復雜選項的時候建議用下面的兩種方法。 2. getopts/getopt 處理命令行參數是一個相似而又復雜的事情,為此,C提供了getopt/getopt_long等函數,C++的boost提供了Options庫,在shell中,處理此事的是getopts和getopt.getopts和getopt功能相似但又不完全相同,其中getopt是獨立的可執行文件,而getopts是由Bash內置的。先來看看參數傳遞的典型用法: ./test.sh -a -b -c : 短選項,各選項不需參數 ./test.sh -abc : 短選項,和上一種方法的效果一樣,只是將所有的選項寫在一起。 ./test.sh -a args -b -c :短選項,其中-a需要參數,而-b -c不需參數。 ./test.sh --a-long=args --b-long :長選項 我們先來看getopts,它不支持長選項。使用getopts非常簡單:--test.sh [vb] #!/bin/bash #選項後面的冒號表示該選項需要參數 while getopts "a:bc" arg do case $arg in a) #參數存在$OPTARG中 echo "a's arg:$OPTARG" ;; b) echo "b" ;; c) echo "c" ;; ?) #當有不認識的選項的時候arg為? echo "unkonw argument" exit 1 ;; esac done 現在就可以使用:./test.sh -a arg -b -c 或./test.sh -a arg -bc 來加載了。 應該說絕大多數腳本使用該函數就可以了,如果需要支持長選項以及可選參數,那麼就需要使用getopt.下面是getopt自帶的一個例子: [vb] #!/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 比如我們使用 ./test -a -b arg arg1 -c 你可以看到,命令行中多了個arg1參數,在經過getopt和set之後,命令行會變為: -a -b arg -c -- arg1 $1指向-a,$2指向-b,$3指向arg,$4指向-c,$5指向--,而多出的arg1則被放到了最後。 3. 總結 豹尾。
Copyright © Linux教程網 All Rights Reserved