设为首页 收藏本站
查看: 974|回复: 0

shell 中 getopts 用法

[复制链接]

尚未签到

发表于 2018-8-21 06:14:33 | 显示全部楼层 |阅读模式
  在写sh脚本的时候,常常需要运行时输入一些数据。之前已经知道用基本的$*,执行的情况,大概就是$0 $1 $2 $3……
  那么,那些系统命令里的参数又是怎么做出来的呢?我们自己的脚本如何搞出来$0
  -$1的效果呢?这就是getopts的作用了。举例如下:
#!/bin/bash  
echo "OPTIND starts at $OPTIND"
  
while getopts ":pq:" optname
  
  do
  
    case "$optname" in
  
      "p")
  
        echo "Option $optname is specified"
  
        ;;
  
      "q")
  
        echo "Option $optname has value $OPTARG"
  
        ;;
  
      "?")
  
        echo "Unknown option $OPTARG"
  
        ;;
  
      ":")
  
        echo "No argument value for option $OPTARG"
  
        ;;
  
      *)
  
      # Should not occur
  
        echo "Unknown error while processing options"
  
        ;;
  
    esac
  
    echo "OPTIND is now $OPTIND"
  
  done
  在使用getopts命令的时候,shell会自动产生两个变量OPTIND和OPTARG。
  OPTIND初始值为1,其含义是下一个待处理的参数的索引。只要存在,getopts命令返回true,所以一般getopts命令使用while循环;并且OPTIND的体现并不明显。
  OPTARG是当getopts获取到其期望的参数后存入的位置。而如果不在其期望内,则$optname被设为?并将该意外值存入OPTARG;如果$optname需要拥有具体设置值而实际却没有,则$optname被设为:并将丢失设置值的optname存入OPTARG;OPTARG就是选项后面跟的参数值,比如/usr/local/nginx/sbin/nginx -s stop 其中stop就是$OPTARG的值。
  对于$optname,可以用后标:来表示是否需要值;而前标:则表示是否开启静默模式。
  案例分析:
  getopts optstring varname [arg ...]
  optstring  option字符串,会逐个匹配
  varname    每次匹配成功的选项
  arg        参数列表,没写时它会取命令行参数列表
  $OPTIND    特殊变量,option index,会逐个递增
  $OPTARG    特殊变量,option argument,不同情况下有不同的值
  细则1:当optstring以”:“开头时,getopts会区分invalid option错误和miss option argument错误。
  invalid option时,varname会被设成?,$OPTARG是出问题的option;
  miss option argument时,varname会被设成:,$OPTARG是出问题的option。
  如果optstring不以”:“开头,invalid option错误和miss option argument错误都会使
  varname被设成?,$OPTARG是出问题的option。
  细则2:当optstring中的字母跟”:“时(比如下面例子中的t),表明该option可接参数,参数(argument)放在$OPTARG中;
  如果缺参数,且optstring是以”:“开头,则varname的值会是:,$OPTARG是该option,
  否则varname的值是?,$OPTARG是该option。(参照细则1)
  首先先来一个例子吧:
[cpp] view plain copy  
[hello@Git shell]$ bash test.sh -a hello
  
this is -a the arg is ! hello
  
[hello@Git shell]$ more test.sh
  
#!/bin/bash
  

  
while getopts "a:" opt; do
  
  case $opt in
  
    a)
  
      echo "this is -a the arg is ! $OPTARG"
  
      ;;
  
    \?)
  
      echo "Invalid option: -$OPTARG"
  
      ;;
  
  esac
  
done



  上面的例子显示了执行的效果和代码。
  getopts的使用形式是:getopts option_string variable
  getopts一共有两个参数,第一个是-a这样的选项,第二个参数是 hello这样的参数。
  选项之间可以通过冒号:进行分隔,也可以直接相连接,:表示选项后面必须带有参数,如果没有可以不加实际值进行传递
  例如:getopts ahfvc: option表明选项a、h、f、v可以不加实际值进行传递,而选项c必须取值。使用选项取值时,必须使用变量OPTARG保存该值。
  
[cpp] view plain copy
  
[hello@Git shell]$ bash test.sh -a hello -b
  
this is -a the arg is ! hello
  
test.sh: option requires an argument -- b
  
Invalid option: -
  
[hello@Git shell]$ bash test.sh -a hello -b hello -c
  
this is -a the arg is ! hello
  
this is -b the arg is ! hello
  
this is -c the arg is !
  
[hello@Git shell]$ more test.sh
  
#!/bin/bash
  

  
while getopts "a:b:cdef" opt; do
  
  case $opt in
  
    a)
  
      echo "this is -a the arg is ! $OPTARG"
  
      ;;
  
    b)
  
      echo "this is -b the arg is ! $OPTARG"
  
      ;;
  
    c)
  
      echo "this is -c the arg is ! $OPTARG"
  
      ;;
  
    \?)
  
      echo "Invalid option: -$OPTARG"
  
      ;;
  
  esac
  
done
  
[hello@Git shell]$



  执行结果结合代码显而易见。同样你也会看到有些代码在a的前面也会有冒号,比如下面的
  情况一,没有冒号:
[cpp] view plain copy  
[hello@Git shell]$ bash test.sh -a hello
  
this is -a the arg is ! hello
  
[hello@Git shell]$ bash test.sh -a
  
test.sh: option requires an argument -- a
  
Invalid option: -
  
[hello@Git shell]$ more test.sh
  
#!/bin/bash
  

  
while getopts "a:" opt; do
  
  case $opt in
  
    a)
  
      echo "this is -a the arg is ! $OPTARG"
  
      ;;
  
    \?)
  
      echo "Invalid option: -$OPTARG"
  
      ;;
  
  esac
  
done
  
[hello@Git shell]$



  情况二,有冒号:

  • [hello@Git shell]$ bash test.sh -a hello  
    this is -a the arg is ! hello
      
    [hello@Git shell]$ bash test.sh -a
      
    [hello@Git shell]$ more test.sh
      
    #!/bin/bash
      

      
    while getopts ":a:" opt; do
      
      case $opt in
      
        a)
      
          echo "this is -a the arg is ! $OPTARG"
      
          ;;
      
        \?)
      
          echo "Invalid option: -$OPTARG"
      
          ;;
      
      esac
      
    done
  情况一输入 -a 但是后面没有参数的的时候,会报错误,但是如果像情况二那样就不会报错误了,会被忽略。
  getopts option_string variable
  当optstring以”:”开头时,getopts会区分invalid option错误和miss option argument错误。
  invalid option时,varname会被设成?,$OPTARG是出问题的option;
  miss option argument时,varname会被设成:,$OPTARG是出问题的option。
  如果optstring不以”:“开头,invalid option错误和miss option argument错误都会使varname被设成?,$OPTARG是出问题的option。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-554388-1-1.html 上篇帖子: shell中使用FTP 下篇帖子: shell 编程备忘
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表