天天看點

shell指令行處理getops/getopt

原文位址:http://www.jb51.net/article/48686.htm

1. 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

#!/bin/bash

while getopts "a:bc" arg #選項後面的冒号表示該選項需要參數

do

        case $arg in

             a)

                echo "a's arg:$optarg" #參數存在$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自帶的一個例子:

複制代碼代碼如下:

#!/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