Shell腳本傳遞參數(shù)的方法對比-創(chuàng)新互聯(lián)

這篇文章主要介紹“Shell腳本傳遞參數(shù)的方法對比”,在日常操作中,相信很多人在Shell腳本傳遞參數(shù)的方法對比問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Shell腳本傳遞參數(shù)的方法對比”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

創(chuàng)新互聯(lián)長期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為建安企業(yè)提供專業(yè)的成都網(wǎng)站制作、網(wǎng)站設(shè)計,建安網(wǎng)站改版等技術(shù)服務(wù)。擁有10余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
#!/bin/bash
#extracting command text_text_text_line options as parameters

help_info(){
  echo "NAME"
  echo "\t$0"
  echo "SYNOPSIS"
  echo "\t$0 is a shell test about process options"
  echo "DESCRIPTION"
  echo "\toption like -a -b param1 -c param2 -d"
}

if [ $# -lt 0 ]
then
  help_info
fi

nomal_opts_act()
{
  echo -e "\n### nomal_opts_act ###\n"

  while [ -n "$1" ]
  do
  case "$1" in 
    -a)
      echo "Found the -a option"
      ;;
    -b)
      echo "Found the -b option"
      echo "The parameter follow -b is $2" 
      shift
      ;;
    -c)
      echo "Found the -c option"
      echo "The parameter follow -c is $2"
      shift
      ;;
    -d)
      echo "Found the -d option"
      ;;
     *)
       echo "$1 is not an option"
      ;;
  esac
  shift
  done
}

#用shell命令自建的選項(xiàng)解析,可以按照自己的想法實(shí)現(xiàn)
#優(yōu)點(diǎn):自己定制,沒有做不到,只有想不到
#缺點(diǎn):麻煩

getopt_act()
{
  echo -e "\n### getopt_act ###\n"

  GETOPTOUT=`getopt ab:c:d "$@"`
  set -- $GETOPTOUT 
  while [ -n "$1" ] 
  do
  case $1 in 
    -a)
      echo "Found the -a option"
      ;;
    -b)
      echo "Found the -b option"
      echo "The parameter follow -b is "$2""
      shift
      ;;
    -c)
      echo "Found the -c option"
      echo "The parameter follow -c is "$2""
      shift
      ;;
    -d)
      echo "Found the -d option"
      ;;
    --)
      shift
      break
      ;;
     *)
       echo "Unknow option: "$1""
      ;;
  esac
  shift
  done

  param_index=1
  for param in "$@"
  do
    echo "Parameter $param_index:$param"
    param_index=$[ $param_index + 1 ] 
  done
}

#用getopt命令解析選項(xiàng)和參數(shù)
#優(yōu)點(diǎn):相對與getopts來說是個半自動解析,自動組織選項(xiàng)和參數(shù),用 -- 符號將選項(xiàng)與參數(shù)隔開
#缺點(diǎn):相對于getopts的缺點(diǎn)
#1.需要與set -- 命令配合,不是必須,需要手動shift
#2.選項(xiàng)參數(shù)中不支持空格如 -a -b dog -c "earth moon" -d -f param1 param2 就會解析錯誤

getopts_act()
{
  echo -e "\n### getopts_act ###\n"
  while getopts :ab:c:d ARGS
  do
  case $ARGS in 
    a)
      echo "Found the -a option"
      ;;
    b)
      echo "Found the -b option"
      echo "The parameter follow -b is $OPTARG"
      ;;
    c)
      echo "Found the -c option"
      echo "The parameter follow -c is $OPTARG"
      ;;
    d)
      echo "Found the -d option"
      ;;
     *)
       echo "Unknow option: $ARGS"
      ;;
  esac
  done

  shift $[ $OPTIND -1 ] 
  param_index=1
  for param in "$@"
  do
    echo "Parameter $param_index:$param"
    param_index=$[ $param_index + 1 ] 
  done
}

#getopts 命令解析選項(xiàng)和參數(shù)
#優(yōu)點(diǎn):可在參數(shù)中包含空格如:-c "earth moon"
#      選項(xiàng)字母和參數(shù)值之間可以沒有空格如:-bdog
#      可將未定義的選項(xiàng)綁定到?輸出
#      Unknow option: ?

nomal_opts_act -a -b dog -c earth -d -f param1 param2
getopts_act -a -b dog -c "earth moon" -d -f param1 param2
getopt_act -a -b dog -c earth -d -f param1 param2

到此,關(guān)于“Shell腳本傳遞參數(shù)的方法對比”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

網(wǎng)站欄目:Shell腳本傳遞參數(shù)的方法對比-創(chuàng)新互聯(lián)
文章地址:http://bm7419.com/article26/ceddjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、網(wǎng)站設(shè)計、動態(tài)網(wǎng)站外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)