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

来源:互联网 发布:ubuntu 安装 sublime 编辑:程序博客网 时间:2024/05/17 05:09
  1. #!/bin/bash  
  2. #extracting command text_text_text_line options as parameters  
  3.   
  4. help_info(){  
  5.     echo "NAME"  
  6.     echo "\t$0"  
  7.     echo "SYNOPSIS"  
  8.     echo "\t$0 is a shell test about process options"  
  9.     echo "DESCRIPTION"  
  10.     echo "\toption like -a -b param1 -c param2 -d"  
  11. }  
  12.   
  13. if [ $# -lt 0 ]  
  14. then  
  15.     help_info  
  16. fi  
  17.   
  18. nomal_opts_act()  
  19. {  
  20.     echo -e "\n### nomal_opts_act ###\n"  
  21.   
  22.     while [ -n "$1" ]  
  23.     do  
  24.     case "$1" in   
  25.         -a)  
  26.             echo "Found the -a option"  
  27.             ;;  
  28.         -b)  
  29.             echo "Found the -b option"  
  30.             echo "The parameter follow -b is $2"   
  31.             shift  
  32.             ;;  
  33.         -c)  
  34.             echo "Found the -c option"  
  35.             echo "The parameter follow -c is $2"  
  36.             shift  
  37.             ;;  
  38.         -d)  
  39.             echo "Found the -d option"  
  40.             ;;  
  41.          *)  
  42.              echo "$1 is not an option"  
  43.             ;;  
  44.     esac  
  45.     shift  
  46.     done  
  47. }  
  48.   
  49. #用shell命令自建的选项解析,可以按照自己的想法实现  
  50. #优点:自己定制,没有做不到,只有想不到  
  51. #缺点:麻烦  
  52.   
  53. getopt_act()  
  54. {  
  55.     echo -e "\n### getopt_act ###\n"  
  56.   
  57.     GETOPTOUT=`getopt ab:c:d "$@"`  
  58.     set -- $GETOPTOUT   
  59.     while [ -n "$1" ]   
  60.     do  
  61.     case $1 in   
  62.         -a)  
  63.             echo "Found the -a option"  
  64.             ;;  
  65.         -b)  
  66.             echo "Found the -b option"  
  67.             echo "The parameter follow -b is "$2""  
  68.             shift  
  69.             ;;  
  70.         -c)  
  71.             echo "Found the -c option"  
  72.             echo "The parameter follow -c is "$2""  
  73.             shift  
  74.             ;;  
  75.         -d)  
  76.             echo "Found the -d option"  
  77.             ;;  
  78.         --)  
  79.             shift  
  80.             break  
  81.             ;;  
  82.          *)  
  83.              echo "Unknow option: "$1""  
  84.             ;;  
  85.     esac  
  86.     shift  
  87.     done  
  88.   
  89.     param_index=1  
  90.     for param in "$@"  
  91.     do  
  92.         echo "Parameter $param_index:$param"  
  93.         param_index=$[ $param_index + 1 ]   
  94.     done  
  95. }  
  96.   
  97. #用getopt命令解析选项和参数  
  98. #优点:相对与getopts来说是个半自动解析,自动组织选项和参数,用 -- 符号将选项与参数隔开  
  99. #缺点:相对于getopts的缺点  
  100. #1.需要与set -- 命令配合,不是必须,需要手动shift  
  101. #2.选项参数中不支持空格如 -a -b dog -c "earth moon" -d -f param1 param2 就会解析错误  
  102.   
  103. getopts_act()  
  104. {  
  105.     echo -e "\n### getopts_act ###\n"  
  106.     while getopts :ab:c:d ARGS  
  107.     do  
  108.     case $ARGS in   
  109.         a)  
  110.             echo "Found the -a option"  
  111.             ;;  
  112.         b)  
  113.             echo "Found the -b option"  
  114.             echo "The parameter follow -b is $OPTARG"  
  115.             ;;  
  116.         c)  
  117.             echo "Found the -c option"  
  118.             echo "The parameter follow -c is $OPTARG"  
  119.             ;;  
  120.         d)  
  121.             echo "Found the -d option"  
  122.             ;;  
  123.          *)  
  124.              echo "Unknow option: $ARGS"  
  125.             ;;  
  126.     esac  
  127.     done  
  128.   
  129.     shift $[ $OPTIND -1 ]   
  130.     param_index=1  
  131.     for param in "$@"  
  132.     do  
  133.         echo "Parameter $param_index:$param"  
  134.         param_index=$[ $param_index + 1 ]   
  135.     done  
  136. }  
  137.   
  138. #getopts 命令解析选项和参数  
  139. #优点:可在参数中包含空格如:-c "earth moon"  
  140. #            选项字母和参数值之间可以没有空格如:-bdog  
  141. #            可将未定义的选项绑定到?输出  
  142. #            Unknow option: ?  
  143.   
  144. nomal_opts_act -a -b dog -c earth -d -f param1 param2  
  145. getopts_act -a -b dog -c "earth moon" -d -f param1 param2  
  146. getopt_act -a -b dog -c earth -d -f param1 param2  
0 0
原创粉丝点击