主頁(yè) > 知識(shí)庫(kù) > Shell腳本傳遞參數(shù)的3種方法比較

Shell腳本傳遞參數(shù)的3種方法比較

熱門(mén)標(biāo)簽:地方門(mén)戶(hù)網(wǎng)站 百度競(jìng)價(jià)排名 Linux服務(wù)器 網(wǎng)站排名優(yōu)化 鐵路電話(huà)系統(tǒng) 服務(wù)外包 AI電銷(xiāo) 呼叫中心市場(chǎng)需求
#!/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):自己定制,沒(méi)有做不到,只有想不到
#缺點(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):相對(duì)與getopts來(lái)說(shuō)是個(gè)半自動(dòng)解析,自動(dòng)組織選項(xiàng)和參數(shù),用 -- 符號(hào)將選項(xiàng)與參數(shù)隔開(kāi)
#缺點(diǎn):相對(duì)于getopts的缺點(diǎn)
#1.需要與set -- 命令配合,不是必須,需要手動(dòng)shift
#2.選項(xiàng)參數(shù)中不支持空格如 -a -b dog -c "earth moon" -d -f param1 param2 就會(huì)解析錯(cuò)誤

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ù)值之間可以沒(méi)有空格如:-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
您可能感興趣的文章:
  • PowerShell函數(shù)中接收管道參數(shù)實(shí)例
  • Shell腳本用for循環(huán)遍歷參數(shù)的方法技巧
  • 一條命令讓你明白shell中read命令的常用參數(shù)
  • 對(duì)shell中常見(jiàn)參數(shù)及判斷命令介紹
  • shell 使用數(shù)組作為函數(shù)參數(shù)的方法(詳解)
  • shell腳本使用兩個(gè)橫杠接收外部參數(shù)的方法

標(biāo)簽:湘潭 仙桃 湖南 黃山 蘭州 衡水 崇左 銅川

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Shell腳本傳遞參數(shù)的3種方法比較》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266