天天看點

腳本樂園 Shell中指令行選項和參數的處理

1.直接處理,依次對$1,$2,...,$n進行解析,分别手工處理;

2.getopts來處理,單個字元選項的情況(如:-n 10 -f file.txt等選項);

3.getopt,可以處理單個字元選項,也可以處理長選項long-option(如:--prefix=/home等)。

下面分别進行簡單的說明:

1.直接手工處理位置參數

必須要要知道幾個變量,

    *    $0 :即指令本身,相當于C/C++中的argv[0]

    *    $1 :第一個參數.

    *    $2, $3, $4 ... :第2、3、4個參數,依次類推。

    *    $#  參數的個數,不包括指令本身

    *    $@ :參數本身的清單,也不包括指令本身

    *    $* :和$@相同,但"$*" 和 "$@"(加引号)并不同,"$*"将所有的參數解釋成一個字元串,而"$@"是一個參數數組。

手工處理方式能滿足多數的簡單需求,配合shift使用也能構造出強大的功能,但處理複雜選項的時候建議用下面的兩種方法。

給個執行個體吧(getargs.sh):

#!/bin/bash

if [ $# -lt 1 ]; then

    echo "error.. need args"

    exit 1

fi

echo "commond is $0"

echo "args are:"

for arg in "$@"

do

    echo $arg

done

運作指令:./getargs.sh 11 22 cc

commond is ./getargs.sh

args are:

11

22

cc

2.getopts (Shell内置指令)

處理指令行參數是一個相似而又複雜的事情,為此,C提供了getopt/getopt_long等函數,C++的boost提供了Options庫,在shell中,處理此事的是getopts和getopt.

先說一下getopts/getopt的差別吧,getopt是個外部binary檔案,而getopts是shell builtin。

[admin@intlqa142055x ~]$ type getopt

getopt is /usr/bin/getopt

[admin@intlqa142055x ~]$ type getopts

getopts is a shell builtin

getopts不能直接處理長的選項(如:--prefix=/home等)

關于getopts的使用方法,可以man bash  搜尋getopts

getopts有兩個參數,第一個參數是一個字元串,包括字元和“:”,每一個字元都是一個有效的選項,如果字元後面帶有“:”,表示這個字元有自己的參數。getopts從指令中擷取這些參數,并且删去了“-”,并将其指派在第二個參數中,如果帶有自己參數,這個參數指派在“OPTARG”中。提供getopts的shell内置了OPTARG這個變變,getopts修改了這個變量。

這裡變量$OPTARG存儲相應選項的參數,而$OPTIND總是存儲原始$*中下一個要處理的元素位置。

while getopts ":a:bc" opt  #第一個冒号表示忽略錯誤;字元後面的冒号表示該選項必須有自己的參數

代碼執行個體(getopts.sh):

echo $*

while getopts ":a:bc" opt

        case $opt in

                a ) echo $OPTARG

                    echo $OPTIND;;

                b ) echo "b $OPTIND";;

                c ) echo "c $OPTIND";;

                ? ) echo "error"

                    exit 1;;

        esac

echo $OPTIND

shift $(($OPTIND - 1))

#通過shift $(($OPTIND - 1))的處理,$*中就隻保留了除去選項内容的參數,可以在其後進行正常的shell程式設計處理了。

echo $0

執行指令:./getopts.sh -a 11 -b -c

-a 11 -b -c

3

b 4

c 5

5

./getopts.sh

3.getopt(一個外部工具)

具體用用法可以 man getopt

#-o表示短選項,兩個冒号表示該選項有一個可選參數,可選參數必須緊貼選項,如-carg 而不能是-c arg

#--long表示長選項

簡單舉個例子吧(getopt.sh):

# 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

#"$@"在上面解釋過

# -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 ;;

echo "Remaining arguments:"

for arg do

   echo '--> '"\`$arg'" ;

運作指令:./getopt.sh --b-long abc -a -c33 remain

Option b, argument `abc'

Option a

Option c, argument `33'

Remaining arguments:

--> `remain'

參考資料:

http://www.cnblogs.com/FrankTan/archive/2010/03/01/1634516.html

http://www.orczhou.com/index.php/2010/04/linux-shell-getopt/

http://blog.csdn.net/flowingflying/archive/2010/01/03/5126066.aspx