国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Linux Shell > 正文

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

2020-07-27 19:06:50
字體:
供稿:網(wǎng)友
#!/bin/bash#extracting command text_text_text_line options as parametershelp_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_infofinomal_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來說是個(gè)半自動(dòng)解析,自動(dòng)組織選項(xiàng)和參數(shù),用 -- 符號將選項(xiàng)與參數(shù)隔開#缺點(diǎn):相對于getopts的缺點(diǎn)#1.需要與set -- 命令配合,不是必須,需要手動(dòng)shift#2.選項(xiàng)參數(shù)中不支持空格如 -a -b dog -c "earth moon" -d -f param1 param2 就會解析錯(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ù)值之間可以沒有空格如:-bdog#      可將未定義的選項(xiàng)綁定到?輸出#      Unknow option: ?nomal_opts_act -a -b dog -c earth -d -f param1 param2getopts_act -a -b dog -c "earth moon" -d -f param1 param2getopt_act -a -b dog -c earth -d -f param1 param2
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 灵台县| 石泉县| 泰兴市| 新宁县| 甘洛县| 汕头市| 永嘉县| 江孜县| 南开区| 灯塔市| 明光市| 友谊县| 辽源市| 鸡东县| 汶川县| 咸宁市| 沁源县| 安新县| 仙游县| 无极县| 宜良县| 汤阴县| 盘山县| 明光市| 高密市| 呼图壁县| 江陵县| 西乡县| 尚志市| 福州市| 宁津县| 湘阴县| 和龙市| 西昌市| 南京市| 渝北区| 界首市| 临澧县| 县级市| 焦作市| 金华市|