shell 腳本參數(shù)解析之 getopt getopts

本文轉(zhuǎn)自:https://www.bbsmax.com/A/ZOJPrxqldv/

一、說(shuō)明

shell中獲取參數(shù)可以直接使用$1、$2等形式來(lái)獲取,但這種方式有明顯的限制:每個(gè)參數(shù)的位置是固定的。比如如果在設(shè)計(jì)上 $1是ip地址$2是端口,那在執(zhí)行時(shí)就必須第一個(gè)參數(shù)是ip第二個(gè)參數(shù)是端口而不能反過(guò)來(lái)。

shell提供了getopt和getopts來(lái)解析參數(shù),getopt比getopts功能強(qiáng)一些getopts比getopt簡(jiǎn)單一些;總體而言getopt和getopts都差強(qiáng)人意。

二、使用getopt解析參數(shù)

getopt比getopts強(qiáng)一些復(fù)雜一些:能在命令行中單獨(dú)使用、支持長(zhǎng)選項(xiàng)格式、支持選項(xiàng)值可選。更多說(shuō)明見(jiàn)注釋。

#/bin/bash
 
usage(){
  echo “
Usage:
  -i, --ip    target server ip
  -p, --port    target service port
  -h, --help    display this help and exit
 
  example1: testGetopt -i192.168.1. -p80
  example2: testGetopt --ip=192.168.1.1 --port=
“
# 短格式中,選項(xiàng)值為可選的選項(xiàng),選項(xiàng)值只能緊接選項(xiàng)而不可使用任何符號(hào)將其他選項(xiàng)隔開(kāi);如-p80,不要寫成性-p
# 短格式中,選項(xiàng)值為必有的選項(xiàng),選項(xiàng)值既可緊接選項(xiàng)也可以使用空格與選項(xiàng)隔開(kāi);如-i192.168.1.,也可寫成-i 192.168.1.1
# 長(zhǎng)格式中,選項(xiàng)值為可選的選項(xiàng),選項(xiàng)值只能使用=號(hào)連接選項(xiàng);如--port=,不可寫成性--port80或--port
# 長(zhǎng)格式中,選項(xiàng)值為必有的選項(xiàng),選項(xiàng)值既可使用=號(hào)連接選項(xiàng)也可使用空格連接選項(xiàng);如--ip=192.168.1.1,也可寫成--ip 192.168.1.1
# 為簡(jiǎn)便起見(jiàn),建議凡是短格式都使用“選項(xiàng)+選項(xiàng)值”的形式(-p80),凡是長(zhǎng)格式都使用“選項(xiàng)+=+選項(xiàng)值”的形式(--port=)
}
 
main(){
while true
do
  case "$1” in
  -i|--ip)
      ip=“$2”
      echo "ip:    $ip”
      shift
      ;;
  -p|--port)
      port=“$2”
      echo "port:    $port”
      shift
      ;;
  -h|--help)
      usage
      # 打印usage之后直接用exit退出程序
      exit
      ;;
  --)
    shift
    break
    ;;
  *)
    echo "$1 is not option”
    ;;
  esac
  shift
done
# 剩余所有未解析到的參數(shù)存在$@中,可通過(guò)遍歷$@來(lái)獲取
#for param in “$@“
#do
#  echo "Parameter #$count: $param”
#done
}
 
# 如果只注冊(cè)短格式可以如下這樣子
# set -- $(getopt i:p::h "$@“)
# 如果要注冊(cè)長(zhǎng)格式需要如下這樣子
# -o注冊(cè)短格式選項(xiàng)
# --long注冊(cè)長(zhǎng)格式選項(xiàng)
# 選項(xiàng)后接一個(gè)冒號(hào)表示其后為其參數(shù)值,選項(xiàng)后接兩個(gè)冒號(hào)表示其后可以有也可以沒(méi)有選項(xiàng)值,選項(xiàng)后沒(méi)有冒號(hào)表示其后不是其參數(shù)值
set -- $(getopt -o i:p::h --long ip:,port::,help -- "$@“)
# 由于是在main函數(shù)中才實(shí)現(xiàn)參數(shù)處理,所以需要使用$@將所有參數(shù)傳到main函數(shù)
main $@

執(zhí)行結(jié)果:


getopt.png

參考:
https://blog.csdn.net/wh211212/article/details/53750366
http://yejinxin.github.io/parse-shell-options-with-getopt-command

三、使用getopts解析參數(shù)
getopts比getopt弱一些簡(jiǎn)單一些:不能在命令行中單獨(dú)使用、不支持長(zhǎng)選項(xiàng)格式、不支持選項(xiàng)值可選。更多說(shuō)明見(jiàn)注釋。

#!/bin/bash
 
usage(){
  echo “
Usage:
  -i, --ip    target server ip
  -p, --port    target service port
  -h, --help    display this help and exit
 
  example1: ./testGetopts.sh -i192.168.1. -p80
  example2: ./testGetopts.sh -i 192.168.1.1 -p
“
# getopts只能在shell腳本中使用,不能像getopt一樣在命令行中單獨(dú)使用
# getopts只支持短格式不支持長(zhǎng)格式
# getopts如果設(shè)定有選項(xiàng)值的選項(xiàng),如果沒(méi)提供選項(xiàng)值那么會(huì)直接報(bào)錯(cuò)
# getopts選項(xiàng)要么有選項(xiàng)值要么沒(méi)有選項(xiàng)值,沒(méi)有可有也可以沒(méi)有
# getopts選項(xiàng)后可緊接選項(xiàng)值,也可以使用空格隔開(kāi);為與getopt統(tǒng)一建議使用緊接格式
 
}
 
main(){
  # 選項(xiàng)有:表示該選項(xiàng)需要選項(xiàng)值
  while getopts "i:p:h” arg
  do
    case $arg in
      i)
         #參數(shù)存在$OPTARG中
         ip=“$OPTARG”
         echo "ip:    $ip”
         ;;
      p)
         port=“$OPTARG”
         echo "port:    $port”
         ;;
      h)
         usage
         # 打印usage之后直接用exit退出程序
         exit
         ;;
      ?)
         #當(dāng)有不認(rèn)識(shí)的選項(xiàng)的時(shí)候arg值為?
         echo "unregistered argument”
         exit
         ;;
    esac
  done
}
 
main $@

執(zhí)行結(jié)果:


getopts.png

參考:
https://www.cnblogs.com/FrankTan/archive/2010/03/01/1634516.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容