<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    so true

    心懷未來,開創未來!
    隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0
    數據加載中……

    bash使用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):
    # ./option_test.sh -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:: -l a-long,b-long:,c-long:: -n 'option_test.sh' -- "$@"`
    echo "TEMP:{$TEMP}"
    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" #必須用eval
    echo "\$#: $#"
    for i in "$@"; do
        echo "{$i}"
    done
    #經過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

    ==================================================================================
    $ set -- a ' b '
    $ echo $#
    2
    $ a="a ' b '"
    $ set -- $a
    $ echo $#
    4
    $ set -- "$a"
    $ echo $#
    1

    posted on 2013-07-11 11:27 so true 閱讀(509) 評論(0)  編輯  收藏 所屬分類: Linux

    主站蜘蛛池模板: 亚洲黄色免费网址| 亚洲码在线中文在线观看| 免费观看AV片在线播放| 免费看无码特级毛片| 国产成人亚洲精品播放器下载| 久久综合亚洲鲁鲁五月天| 国产美女亚洲精品久久久综合| 国产精品无码一区二区三区免费 | 午夜国产羞羞视频免费网站| 1000部国产成人免费视频| 成全视频在线观看免费| 国产精品黄页免费高清在线观看| 亚洲国产精品美女久久久久| 四虎必出精品亚洲高清| 亚洲中文无码线在线观看| 亚洲电影一区二区| 亚洲国产三级在线观看| 最新精品亚洲成a人在线观看| 四虎影在线永久免费四虎地址8848aa| 免费看美女被靠到爽的视频| 日韩一区二区a片免费观看| 中文字幕免费在线看线人 | 亚洲狠狠狠一区二区三区| 亚洲短视频男人的影院| 亚洲av无码国产精品色午夜字幕| 亚洲熟妇丰满多毛XXXX| 日韩一卡2卡3卡4卡新区亚洲 | 亚洲国产免费综合| 污污污视频在线免费观看| 免费国产a理论片| 成人免费网站久久久| 一区二区免费国产在线观看 | 亚洲线精品一区二区三区影音先锋| 亚洲国产综合人成综合网站| 亚洲国产精品一区二区三区久久| 免费播放春色aⅴ视频| 亚洲精品久久久www| 亚洲午夜日韩高清一区| 亚洲中文字幕无码不卡电影| 中文字幕日韩亚洲| 亚洲区小说区图片区QVOD|