欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Shell脚本传递参数的3种方法比较

程序员文章站 2023-09-03 10:42:25
#!/bin/bash #extracting command text_text_text_line options as parameters help...
#!/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