婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

Shell腳本傳遞參數的3種方法比較

熱門標簽:衛星地圖標注地名 聯通電話機器人怎么接 奧維互動地圖標注參數 申請公司400電話要注意什么 安裝外呼系統費用 曲阜400電話辦理 地圖標注輻射圖案 寧波智能外呼系統公司 電銷機器人 劍魚
#!/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命令自建的選項解析,可以按照自己的想法實現
#優點:自己定制,沒有做不到,只有想不到
#缺點:麻煩

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

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

巨人網絡通訊聲明:本文標題《Shell腳本傳遞參數的3種方法比較》,本文關鍵詞  Shell,腳本,傳遞,參數,的,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Shell腳本傳遞參數的3種方法比較》相關的同類信息!
  • 本頁收集關于Shell腳本傳遞參數的3種方法比較的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 元谋县| 新源县| 保靖县| 元阳县| 钦州市| 毕节市| 桦南县| 南雄市| 张北县| 曲阳县| 大埔区| 易门县| 鄂州市| 正定县| 沈丘县| 军事| 抚松县| 五峰| 怀集县| 长阳| 大邑县| 乌拉特中旗| 合山市| 东莞市| 星子县| 岑溪市| 准格尔旗| 靖边县| 佛学| 绿春县| 安阳县| 修水县| 松滋市| 济阳县| 同德县| 华坪县| 凤翔县| 辛集市| 仁化县| 麦盖提县| 平遥县|