加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell getopts 用法

发布时间:2020-12-15 09:20:25 所属栏目:安全 来源:网络整理
导读:?? shell getopts 用法 原创 2013年12月22日 22:05:38 http://www.jb51.cc/article/p-gnkhbtiz-vg.html 标签: linux / shell c语言里面有个 getopt_l ong,可以获取用户在命令下的参数,然后根据参数进行不同的提示或者不同的执行。 在shell中同样有这样的
??
shell getopts 用法
原创 2013年12月22日 22:05:38 http://www.52php.cn/article/p-gnkhbtiz-vg.html
  • 标签:
  • linux /
  • shell
c语言里面有个 getopt_long,可以获取用户在命令下的参数,然后根据参数进行不同的提示或者不同的执行。
在shell中同样有这样的函数或者用法吧,在shell里面是getopts,也有一个getopt是一个比较老的。这次说getopts,我自己的一些用法和感悟。
首先先来一个例子吧:
[cpp] view plain copy print?
  1. [hello@Gitshell]$bashtest.sh-ahello
  2. thisis-atheargis!hello
  3. [hello@Gitshell]$moretest.sh
  4. #!/bin/bash
  5. whilegetopts"a:"opt;do
  6. case$optin
  7. a)
  8. echo"thisis-atheargis!$OPTARG"
  9. ;;
  10. ?)
  11. echo"Invalidoption:-$OPTARG"
  12. ;;
  13. esac
  14. done
上面的例子显示了执行的效果和代码。
getopts的使用形式是:getopts option_string variable
getopts一共有两个参数,第一个是-a这样的选项,第二个参数是 hello这样的参数。
选项之间可以通过冒号 :进行分隔,也可以直接相连接,:表示选项后面必须带有参数,如果没有可以不加实际值进行传递
例如:getopts ahfvc: option表明选项a、h、f、v可以不加实际值进行传递,而选项c必须取值。使用选项取值时,必须使用变量OPTARG保存该值。
[cpp] print?
  1. [hello@Gitshell]$bashtest.sh-ahello-b
  2. thisis-atheargis!hello
  3. test.sh:optionrequiresanargument--b
  4. Invalidoption:-
  5. [hello@Gitshell]$bashtest.sh-ahello-bhello-c
  6. thisis-atheargis!hello
  7. thisis-btheargis!hello
  8. thisis-ctheargis!
  9. [hello@Gitshell]$moretest.sh
  10. #!/bin/bash
  11. whilegetopts"a:b:cdef"opt;do
  12. case$optin
  13. a)
  14. echo"thisis-atheargis!$OPTARG"
  15. ;;
  16. b)
  17. echo"thisis-btheargis!$OPTARG"
  18. ;;
  19. c)
  20. echo"thisis-ctheargis!$OPTARG"
  21. ;;
  22. ?)
  23. echo"Invalidoption:-$OPTARG"
  24. ;;
  25. esac
  26. done
  27. [hello@Gitshell]$
执行结果结合代码显而易见。同样你也会看到有些代码在a的前面也会有冒号,比如下面的
情况一,没有冒号:
[cpp] print?
  1. [hello@Gitshell]$bashtest.sh-ahello
  2. thisis-atheargis!hello
  3. [hello@Gitshell]$bashtest.sh-a
  4. test.sh:optionrequiresanargument--a
  5. Invalidoption:-
  6. [hello@Gitshell]$moretest.sh
  7. #!/bin/bash
  8. whilegetopts"a:"opt;do
  9. case$optin
  10. a)
  11. echo"thisis-atheargis!$OPTARG"
  12. ;;
  13. ?)
  14. echo"Invalidoption:-$OPTARG"
  15. ;;
  16. esac
  17. done
  18. [hello@Gitshell]$

情况二,有冒号:
[cpp] print?
  1. [hello@Gitshell]$bashtest.sh-ahello
  2. thisis-atheargis!hello
  3. [hello@Gitshell]$bashtest.sh-a
  4. [hello@Gitshell]$moretest.sh
  5. #!/bin/bash
  6. whilegetopts":a:"opt;do
  7. case$optin
  8. a)
  9. echo"thisis-atheargis!$OPTARG"
  10. ;;
  11. ?)
  12. echo"Invalidoption:-$OPTARG"
  13. ;;
  14. esac
  15. 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。
参考自: http://xingwang-ye.iteye.com/blog/1601246

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读