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

          主站蜘蛛池模板: 广南县| 双鸭山市| 陆川县| 旬邑县| 满城县| 舟曲县| 梨树县| 大丰市| 罗山县| 民勤县| 福州市| 泰宁县| 开远市| 临沧市| 北流市| 搜索| 晋中市| 乌拉特后旗| 九台市| 惠东县| 阳春市| 济阳县| 西华县| 鞍山市| 沁源县| 措勤县| 江油市| 阿荣旗| 玛曲县| 临清市| 武夷山市| 满洲里市| 旬阳县| 天津市| 余干县| 阳山县| 永德县| 康马县| 敦煌市| 油尖旺区| 新营市|