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 閱讀(514) 評論(0)  編輯  收藏 所屬分類: Linux

          主站蜘蛛池模板: 榆林市| 南漳县| 北京市| 洪湖市| 布尔津县| 紫云| 南靖县| 上栗县| 新龙县| 江孜县| 石河子市| 肥西县| 温州市| 南涧| 元谋县| 阆中市| 绵阳市| 德保县| 博湖县| 彝良县| 金门县| 库车县| 安义县| 都兰县| 乐业县| 连城县| 新和县| 金寨县| 大理市| 安图县| 利川市| 凉城县| 上林县| 延边| 万载县| 苍梧县| 井研县| 通江县| 德钦县| 贵定县| 山丹县|