好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

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

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

熱門標簽:衛(wèi)星地圖標注地名 聯(lián)通電話機器人怎么接 奧維互動地圖標注參數(shù) 申請公司400電話要注意什么 安裝外呼系統(tǒng)費用 曲阜400電話辦理 地圖標注輻射圖案 寧波智能外呼系統(tǒ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àn)
#優(yōu)點:自己定制,沒有做不到,只有想不到
#缺點:麻煩

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命令解析選項和參數(shù)
#優(yōu)點:相對與getopts來說是個半自動解析,自動組織選項和參數(shù),用 -- 符號將選項與參數(shù)隔開
#缺點:相對于getopts的缺點
#1.需要與set -- 命令配合,不是必須,需要手動shift
#2.選項參數(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 命令解析選項和參數(shù)
#優(yōu)點:可在參數(shù)中包含空格如:-c "earth moon"
#      選項字母和參數(shù)值之間可以沒有空格如:-bdog
#      可將未定義的選項綁定到?輸出
#      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ù)實例
  • Shell腳本用for循環(huán)遍歷參數(shù)的方法技巧
  • 一條命令讓你明白shell中read命令的常用參數(shù)
  • 對shell中常見參數(shù)及判斷命令介紹
  • shell 使用數(shù)組作為函數(shù)參數(shù)的方法(詳解)
  • shell腳本使用兩個橫杠接收外部參數(shù)的方法

標簽:安康 大興安嶺 大慶 仙桃 遵義 江西 三門峽 上饒

巨人網(wǎng)絡通訊聲明:本文標題《Shell腳本傳遞參數(shù)的3種方法比較》,本文關鍵詞  Shell,腳本,傳遞,參數(shù),的,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Shell腳本傳遞參數(shù)的3種方法比較》相關的同類信息!
  • 本頁收集關于Shell腳本傳遞參數(shù)的3種方法比較的相關信息資訊供網(wǎng)民參考!
  • 推薦文章