<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

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

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    LinuxShell中怎樣處理tail -n 10 access.log這樣的命令行選項呢?這是被別人問起的一個問題,好好學習了一下,進行總結如下:
    在bash中,可以用以下三種方式來處理命令行參數,每種方式都有自己的應用場景。
    1.直接處理,依次對$1,$2,...,$n進行解析,分別手工處理;
    2.getopts來處理,單個字符選項的情況(如:-n 10 -f file.txt等選項);
    3.getopt,可以處理單個字符選項,也可以處理長選項long-option(如:--prefix=/home等)。
    總結:一般小腳本手工處理也許就夠了,getopts能處理絕大多數的情況,getopt較復雜、功能也更強大。

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

    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
    do
            case $opt in
                    a ) echo $OPTARG
                        echo $OPTIND;;
                    b ) echo "b $OPTIND";;
                    c ) echo "c $OPTIND";;
                    ? ) echo "error"
                        exit 1;;
            esac
    done
    echo $OPTIND
    shift $(($OPTIND - 1))
    #通過shift $(($OPTIND - 1))的處理,$*中就只保留了除去選項內容的參數,可以在其后進行正常的shell編程處理了。
    echo $0
    echo $*

    執行命令:./getopts.sh -a 11 -b -c
    -a 11 -b -c
    11
    3
    b 4
    c 5
    5
    ./getopts.sh

    3.getopt(一個外部工具)
    具體用用法可以 man getopt
    #-o表示短選項,兩個冒號表示該選項有一個可選參數,可選參數必須緊貼選項,如-carg 而不能是-c arg
    #--long表示長選項
    簡單舉個例子吧(getopt.sh):


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

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全