zoukankan      html  css  js  c++  java
  • shell getopt getopts获取参数

    1. #!/bin/sh
    2. #说明
    3. show_usage="args: [-i , -p , -u , -w , -a , -s , -d , -v ]
    4.                   [--ip=, --port=, --user=, --pwd=, --path=, --script=, --debug=, --version=]"
    5. #参数
    6. opt_ip=""
    7. opt_port=""
    8. opt_user=""
    9. opt_pwd=""
    10. opt_path=""
    11. opt_script=""
    12. opt_debug=""
    13. opt_version=""
    14. GETOPT_ARGS=`getopt -o i:p:u:w:a:s:d:v: -al ip:,port:,user:,pwd:,path:,script:,debug:,version: -- "$@"`
    15. eval set -- "$GETOPT_ARGS"
    16. #获取参数
    17. while [ -n "$1" ]
    18. do
    19.     case "$1" in
    20.         -i|--ip) opt_ip=$2; shift 2;;
    21.         -p|--port) opt_port=$2; shift 2;;
    22.         -u|--user) opt_user=$2; shift 2;;
    23.         -w|--pwd) opt_pwd=$2; shift 2;;
    24.         -a|--path) opt_path=$2; shift 2;;
    25.         -s|--script) opt_script=$2; shift 2;;
    26.         -d|--debug) opt_debug=$2; shift 2;;
    27.         -v|--version) opt_version=$2; shift 2;;
    28.         --) break ;;
    29.         *) echo $1,$2,$show_usage; break ;;
    30.     esac
    31. done
    32.  
    33. if [[ -z $opt_ip || -z $opt_port || -z $opt_user || -z $opt_pwd || -z $opt_path || -z $opt_script || -z $opt_debug || -z $opt_version ]]; then
    34.     echo $show_usage
    35.     echo "opt_ip:"$opt_ip",opt_port:"$opt_port",opt_user:"$opt_user",opt_pwd:"$opt_pwd",opt_path:"$opt_path",opt_script:"$opt_script",opt_debug:"$opt_debug",opt_version:"$opt_version
    36.     exit 0
    37. fi
    38.  
    39. #开始处理
    40. #ip port user pwd 连接服务器
    41. #script path debug version 作为参数执行
    42. 来源: http://my.oschina.net/u/659405/blog/467855
    1. 一、找出选项
    2. 1、处理简单选项 在抽取每个参数是,使用case语句判断参数是否符合选项格式
    3. [root@rac2 ~]# cat t3.sh
    4. #!/bin/bash
    5. while [ -n "$1" ] ---此处的$1必须加且只能用双引号
    6. do
    7. case $1 in ---此处的$1可以加双引号或不加
    8. -a) echo "found the -a option";; ---此处的两个分号一定要加上
    9. -b) echo "found the -b option";;
    10. -c) echo "found the -c option";;
    11. *) echo "$1 is not an option";;
    12. esac
    13. shift
    14. done
    15. [root@rac2 ~]# ./t3.sh -a -b -c -g
    16. found the -a option
    17. found the -b option
    18. found the -c option
    19. -g is not an option
    20. case语句检查每个参数是否为有效的选项,当找到一个选项时,就在case语句中运行适当的命令。
    21. 2、从参数中分离选项
    22. linux使用特殊字符码将选项和普通参数分开,这个字符码告诉脚本选项结束和普通参数开始的位置。所以发现双破折号后,
    23. 脚本就能够安全的将剩余的命令行参数作为参数而不是选项来处理。
    24. [root@rac2 ~]# cat t4.sh
    25. #!/bin/bash
    26. while [ -n "$1" ]
    27. do
    28. case $1 in
    29. -a) echo "found the -a option";;
    30. -b) echo "found the -b option";;
    31. -c) echo "found the -c option";;
    32. --) shift
    33. break ;;
    34. *) echo "$1 is not an option";;
    35. esac
    36. shift
    37. done
    38. count=1
    39. for param in $@
    40. do
    41. echo "parameter #count:$param"
    42. count=$[ $count + 1 ]
    43. done
    44. [root@rac2 ~]# ./t4.sh
    45. [root@rac2 ~]# ./t4.sh -a -v -b -- test1 test2 test3 ---当脚本到达双破折号是,停止处理选项,并将其余的参数视为命令行参数
    46. found the -a option
    47. -v is not an option
    48. found the -b option
    49. parameter #count:test1
    50. parameter #count:test2
    51. parameter #count:test3
    52. 3、处理带值的选项
    53. [root@rac2 ~]# cat t5.sh
    54. #!/bin/bash
    55. while [ -n "$1" ]
    56. do
    57. case $1 in
    58. -a) echo "found the -a option";;
    59. -b) param="$2"
    60. echo "found the -b option,with parameter value $param"
    61. shift 1;;
    62. -c) echo "found the -c option";;
    63. --) shift
    64. break;;
    65. esac
    66. shift
    67. done
    68. count=1
    69. for param in "$@"
    70. do
    71. echo "parameter #count:$param"
    72. count=$[ $count + 1 ]
    73. done
    74. [root@rac2 ~]# ./t5.sh -a -b 56 -c -- test1 test2 test3
    75. found the -a option
    76. found the -b option,with parameter value 56
    77. found the -c option
    78. parameter #count:test1
    79. parameter #count:test2
    80. parameter #count:test3
    81. 本例中-b有附加的参数值,由于要处理的参数是$1.所以知道附加参数位于$2,于是从变量$2中抽取参数值。
    82. 二、getopt命令
    83. getopt命令是个不错的工具,在处理命令行选项时非常的方便,它对命令行参数进行重新的组织,使其更便于在脚本中解析。
    84. 1、命令格式
    85. getopt options optstring parameters
    86. 其中optstring是处理的关键,他定义命令行中有效的选项字母,然后,在每个需要参数值的选项字母后面放置一个冒号。
    87. [root@rac2 ~]# getopt ab:cd -a -b test1 -cd test2 test3
    88. -a -b test1 -c -d -- test2 test3
    89. 如果指定的选项不包含在选项字符串内,getopt命令会默认生成一个错误信息
    90. [root@rac2 ~]# getopt ab:cd -a -b test1 -cde test2 test3
    91. getopt:无效选项 -- e
    92. -a -b test1 -c -d -- test2 test3
    93. 如果希望忽略这个错误消息,可以再命令中使用-q选项:
    94. [root@rac2 ~]# getopt -q ab:cd -a -b test1 -cde test2 test3
    95. -a -b 'test1' -c -d -- 'test2' 'test3'
    96. 2、在脚本中使用getopt
    97. 使用set命令可以讲命令行参数变量替换为set命令的的命令行中的值。
    98. set -- `getopt -q ab:cd "$@"`
    99. [root@rac2 ~]# cat t7.sh
    100. #!/bin/bash
    101. set -- `getopt -q ab:c "$@"`
    102. while [ -n "$1" ]
    103. do
    104. case $1 in
    105. -a) echo "found the -a option";;
    106. -b) param="$2"
    107. echo "found -b option with parameter value $param"
    108. shift;;
    109. -c) echo "found the -c option";;
    110. --) shift
    111. break;;
    112. *) echo "$1 is not an option";;
    113. esac
    114. shift
    115. done
    116. count=1
    117. for param in "$@"
    118. do
    119. echo "parameter #$count:$param"
    120. count=$[ $count + 1 ]
    121. done
    122. [root@rac2 ~]# ./t7.sh -ac
    123. found the -a option
    124. found the -c option
    125. [root@rac2 ~]# ./t7.sh -a -b zhou -cd test1 test2 test3
    126. found the -a option
    127. found -b option with parameter value 'zhou'
    128. found the -c option
    129. parameter #1:'test1'
    130. parameter #2:'test2'
    131. parameter #3:'test3'
    132. 如果命令行参数中存在空格就会出现下面的情况
    133. [root@rac2 ~]# ./t7.sh -a -b zhou -cd "test1 test2" test3
    134. found the -a option
    135. found -b option with parameter value 'zhou'
    136. found the -c option
    137. parameter #1:'test1
    138. parameter #2:test2'
    139. parameter #3:'test3'
    140. getopt命令不能很好的处理带空格的参数值,它将空格解析为参数分隔符,而不是将双引号引起来的两个值合并为一个参数。
    141. 解决上面的问题可以使用getopts命令





  • 相关阅读:
    参加过的面试题目总结
    小论文实验讨论——不同的分类算法
    【设计模式】行为型07备忘录模式(Memento Pattern)
    【设计模式】行为型06命令模式(Command Pattern)
    【设计模式】行为型05责任链模式(Chain of responsibility Pattern)
    【设计模式】行为型04迭代器模式(Iterator Pattern)
    【设计模式】行为型03观察者模式(Observer Pattern)
    【设计模式】行为型02模板方法模式(Template Method Patten)
    【JVM】02垃圾回收机制
    【死磕线程】线程同步机制_java多线程之线程锁
  • 原文地址:https://www.cnblogs.com/gyming/p/5781169.html
Copyright © 2011-2022 走看看