shell getopts 用法
来源:互联网 发布:疑心暗鬼动作数据下载 编辑:程序博客网 时间:2024/05/29 13:14
C语言里面有个getopt_long,可以获取用户在命令下的参数,然后根据参数进行不同的提示或者不同的执行。
在shell中同样有这样的函数或者用法吧,在shell里面是getopts,也有一个getopt是一个比较老的。这次说getopts,我自己的一些用法和感悟。
首先先来一个例子吧:
[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保存该值。
[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的前面也会有冒号,比如下面的
情况一,没有冒号:
[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会被设成?,
如果optstring不以”:“开头,invalid option错误和miss option argument错误都会使varname被设成?,$OPTARG是出问题的option。
参考:http://xingwang-ye.iteye.com/blog/1601246
http://blog.csdn.net/xluren/article/details/17489667
- shell 的getopts用法
- shell getopts 用法
- SHELL:getopts用法介绍
- shell getopts用法详解
- shell getopts 用法
- shell getopts 用法
- shell getopts 用法
- shell getopts 用法
- shell getopts 用法
- shell - getopts的实效用法
- shell getopts
- shell-----getopts
- shell--getopts
- getopts用法
- shell's getopts
- shell之getopts
- Linux shell getopts 笔记
- linux shell getopts
- RMQ
- Codeforces Round #423 (Div. 2) B Black Square 黑色正方形 思维题
- 3DES加密,秘钥长度为32
- 解决maven下载慢的问题
- FM(因子分解机系列)
- shell getopts 用法
- table中的某一格字数太多,要换行
- ORACLE 不走索引(失效)的原因以及解决办法
- js删除数组元素
- js call()与apply()
- 4143:和为给定数
- gitbook 生成 html 电子书, 搭建在 github 或 本地服务器上
- java-(继承、子父类中的变量的特点、子父类中函数的特点-覆盖、)
- 51nod 1640 天气晴朗的魔法 【二分枚举最大生成树】or【最小&&最大 生成树】