十年專注于品牌網(wǎng)站建設(shè) 十余年專注于網(wǎng)站建設(shè)_小程序開(kāi)發(fā)_APP開(kāi)發(fā),低調(diào)、敢創(chuàng)新、有情懷!
      南昌百恒網(wǎng)絡(luò)微信公眾號(hào) 掃一掃關(guān)注
      小程序
      tel-icon全國(guó)服務(wù)熱線:400-680-9298,0791-88117053
      掃一掃關(guān)注百恒網(wǎng)絡(luò)微信公眾號(hào)
      掃一掃打開(kāi)百恒網(wǎng)絡(luò)微信小程序

      百恒網(wǎng)絡(luò)

      南昌百恒網(wǎng)絡(luò)

      介紹Linux中更高級(jí)getopts命令的用法

      百恒網(wǎng)絡(luò) 2017-09-16 7503

      昨天為大家介紹了getopt 命令的使用方法,今天南昌網(wǎng)絡(luò)公司-百恒網(wǎng)絡(luò)小編將為大家介紹一種更高級(jí) getopts命令的用法,供大家參考,學(xué)習(xí)。 getopts命令(注意是復(fù)數(shù))內(nèi)建于bash shell。它跟近親getopt看起來(lái)很像,但多了一些擴(kuò)展功能。

      與getopt不同,前者將命令行上選項(xiàng)和參數(shù)處理后只生成一個(gè)輸出,而getopts命令能夠和已有的shell參數(shù)變量配合默契。

      每次調(diào)用它時(shí),它一次只處理命令行上檢測(cè)到的一個(gè)參數(shù)。處理完所有的參數(shù)后,它會(huì)退出并返回一個(gè)大于0的退出狀態(tài)碼。這讓它非常適合用解析命令行所有參數(shù)的循環(huán)中。

      getopts命令的格式如下:

      getopts optstring variable

      optstring值類似于getopt命令中的那個(gè)。有效的選項(xiàng)字母都會(huì)列在optstring中,如果選項(xiàng)字母要求有個(gè)參數(shù)值,就加一個(gè)冒號(hào)。要去掉錯(cuò)誤消息的話,可以在optstring之前加一個(gè)冒號(hào)。getopts命令將當(dāng)前參數(shù)保存在命令行中定義的variable中。

      getopts命令會(huì)用到兩個(gè)環(huán)境變量。如果選項(xiàng)需要跟一個(gè)參數(shù)值,OPTARG環(huán)境變量就會(huì)保存這個(gè)值。OPTIND環(huán)境變量保存了參數(shù)列表中g(shù)etopts正在處理的參數(shù)位置。這樣你就能在處理完選項(xiàng)之后繼續(xù)處理其他命令行參數(shù)了。

      下面百恒網(wǎng)絡(luò)小編為大家舉個(gè)使用getopts命令的例子,大家可以簡(jiǎn)單了解一下。

      $ cat test19.sh

      #!/bin/bash

      # simple demonstration of the getopts command

      #

      echo

      while getopts :ab:c opt

      do

      case "$opt" in

      a) echo "Found the -a option" ;;

      b) echo "Found the -b option, with value $OPTARG";;

      c) echo "Found the -c option" ;;

      *) echo "Unknown option: $opt";;

      esac

      done

      $

      $ ./test19.sh -ab test1 -c

      Found the -a option

      Found the -b option, with value test1

      Found the -c option

      $

      while語(yǔ)句定義了getopts命令,指明了要查找哪些命令行選項(xiàng),以及每次迭代中存儲(chǔ)它們 的變量名(opt)。

      你會(huì)注意到在本例中case語(yǔ)句的用法有些不同。getopts命令解析命令行選項(xiàng)時(shí)會(huì)移除開(kāi)頭的單破折線,所以在case定義中不用單破折線。

      getopts命令有幾個(gè)好用的功能。對(duì)新手來(lái)說(shuō),可以在參數(shù)值中包含空格。

      $ ./test19.sh -b "test1 test2" -a

      Found the -b option, with value test1 test2

      Found the -a option

      $

      另一個(gè)好用的功能是將選項(xiàng)字母和參數(shù)值放在一起使用,而不用加空格。

      $ ./test19.sh -abtest1

      Found the -a option

      Found the -b option, with value test1

      $

      getopts命令能夠從-b選項(xiàng)中正確解析出test1值。除此之外,getopts還能夠?qū)⒚钚猩险业降乃形炊x的選項(xiàng)統(tǒng)一輸出成問(wèn)號(hào)。

      $ ./test19.sh -d

      Unknown option: ?

      $

      $ ./test19.sh -acde

      Found the -a option

      Found the -c option

      Unknown option: ?

      Unknown option: ?

      $

      optstring中未定義的選項(xiàng)字母會(huì)以問(wèn)號(hào)形式發(fā)送給代碼。

      getopts命令知道何時(shí)停止處理選項(xiàng),并將參數(shù)留給你處理。在getopts處理每個(gè)選項(xiàng)時(shí),它會(huì)將OPTIND環(huán)境變量值增一。在getopts完成處理時(shí),你可以使用shift命令和OPTIND值來(lái)移動(dòng)參數(shù)。

      $ cat test20.sh

      #!/bin/bash

      # Processing options & parameters with getopts

      #

      echo

      while getopts :ab:cd opt

      do

      case "$opt" in

      a) echo "Found the -a option" ;;

      b) echo "Found the -b option, with value $OPTARG" ;;

      c) echo "Found the -c option" ;;

      d) echo "Found the -d option" ;;

      *) echo "Unknown option: $opt" ;;

      esac

      done

      #

      shift $[ $OPTIND - 1 ]

      #

      echo

      count=1

      for param in "$@"

      do

      echo "Parameter $count: $param"

      count=$[ $count + 1 ]

      done

      #

      $

      $ ./test20.sh -a -b test1 -d test2 test3 test4

      Found the -a option

      Found the -b option, with value test1

      Found the -d option

      Parameter 1: test2

      Parameter 2: test3

      Parameter 3: test4

      $

      現(xiàn)在你就擁有了一個(gè)能在所有shell腳本中使用的全功能命令行選項(xiàng)和參數(shù)處理工具。

      關(guān)于更高級(jí)的 getopts命令的使用方法,南昌網(wǎng)絡(luò)公司-百恒網(wǎng)絡(luò)就為大家介紹到這里了,如果還有哪些不太明白的地方,可倒回去再看一遍,或者來(lái)電咨詢我們。此外,如有需要網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)等方面的服務(wù)的朋友,歡迎來(lái)電和我們聯(lián)系,百恒將隨時(shí)為您效勞!

      400-680-9298,0791-88117053
      掃一掃關(guān)注百恒網(wǎng)絡(luò)微信公眾號(hào)
      掃一掃打開(kāi)百恒網(wǎng)絡(luò)小程序

      歡迎您的光顧,我們將竭誠(chéng)為您服務(wù)×

      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售前咨詢 售前咨詢
       
      售后服務(wù) 售后服務(wù)
       
      售后服務(wù) 售后服務(wù)
       
      備案專線 備案專線
       
      ×
      手机日韩精品视频在线看网站| 热久久精品免费视频| 久久亚洲国产午夜精品理论片| jazzjazz国产精品一区二区| 日韩a无v码在线播放| 国产精品久久久久影院| 婷婷99视频精品全部在线观看| 99久久99久久久精品齐齐| 久久精品国产亚洲AV久| 97精品伊人久久久大香线焦| 99精品视频免费观看| 久久久精品人妻一区二区三区| 久久这里只有精品首页| 精品久久久久久国产| 亚洲线精品一区二区三区影音先锋| 久久精品综合一区二区三区| 国产乱人伦偷精品视频不卡| 亚洲第一区精品观看| 日产国产精品亚洲系列| 精品人妻无码专区在中文字幕| 亚洲日韩av无码中文| 日韩黄a级成人毛片| 日韩大乳视频中文字幕| 日韩一区二区免费视频| 色哟哟国产精品免费观看| 夜夜精品无码一区二区三区| 亚洲精品国产电影| 国产精品热久久毛片| 精品视频一区二区三区四区五区| 国产精品户外野外| 久久久久久久久久免免费精品| a级国产精品片在线观看| 奇米精品一区二区三区在| 国产成人精品综合久久久久| 国产精品亚洲а∨无码播放| 国产精品熟女高潮视频| 久久久影院亚洲精品| 久久精品国产亚洲av影院| 久久九九兔免费精品6| 国产精品亚洲四区在线观看| 99精品国产免费久久久久久下载|