shell getopts 用法


shell getopts 用法
原创 2013年12月22日 22:05:38 http://blog.csdn.net/xluren/article/details/17489667
c语言里面有个 getopt_long,可以获取用户在命令下的参数,然后根据参数进行不同的提示或者不同的执行。
在shell中同样有这样的函数或者用法吧,在shell里面是getopts,也有一个getopt是一个比较老的。这次说getopts,我自己的一些用法和感悟。
首先先来一个例子吧:
  1. [hello@Git shell]$ bash test.sh -a hello  
  2. this is -a the arg is ! hello  
  3. [hello@Git shell]$ more test.sh   
  4. #!/bin/bash  
  5.    
  6. while getopts "a:" opt; do  
  7.   case $opt in  
  8.     a)  
  9.       echo "this is -a the arg is ! $OPTARG"   
  10.       ;;  
  11.     \?)  
  12.       echo "Invalid option: -$OPTARG"   
  13.       ;;  
  14.   esac  
  15. done  
上面的例子显示了执行的效果和代码。
getopts的使用形式是:getopts option_string variable 
getopts一共有两个参数,第一个是-a这样的选项,第二个参数是 hello这样的参数。
选项之间可以通过冒号 :进行分隔,也可以直接相连接,:表示选项后面必须带有参数,如果没有可以不加实际值进行传递
例如:getopts ahfvc: option表明选项a、h、f、v可以不加实际值进行传递,而选项c必须取值。使用选项取值时,必须使用变量OPTARG保存该值。
  1. [hello@Git shell]$ bash test.sh -a hello -b  
  2. this is -a the arg is ! hello  
  3. test.sh: option requires an argument -- b  
  4. Invalid option: -  
  5. [hello@Git shell]$ bash test.sh -a hello -b hello -c   
  6. this is -a the arg is ! hello  
  7. this is -b the arg is ! hello  
  8. this is -c the arg is !   
  9. [hello@Git shell]$ more test.sh   
  10. #!/bin/bash  
  11.    
  12. while getopts "a:b:cdef" opt; do  
  13.   case $opt in  
  14.     a)  
  15.       echo "this is -a the arg is ! $OPTARG"   
  16.       ;;  
  17.     b)  
  18.       echo "this is -b the arg is ! $OPTARG"   
  19.       ;;  
  20.     c)  
  21.       echo "this is -c the arg is ! $OPTARG"   
  22.       ;;  
  23.     \?)  
  24.       echo "Invalid option: -$OPTARG"   
  25.       ;;  
  26.   esac  
  27. done  
  28. [hello@Git shell]$   
执行结果结合代码显而易见。同样你也会看到有些代码在a的前面也会有冒号,比如下面的
情况一,没有冒号:
  1. [hello@Git shell]$ bash test.sh -a hello  
  2. this is -a the arg is ! hello  
  3. [hello@Git shell]$ bash test.sh -a  
  4. test.sh: option requires an argument -- a  
  5. Invalid option: -  
  6. [hello@Git shell]$ more test.sh   
  7. #!/bin/bash  
  8.    
  9. while getopts "a:" opt; do  
  10.   case $opt in  
  11.     a)  
  12.       echo "this is -a the arg is ! $OPTARG"   
  13.       ;;  
  14.     \?)  
  15.       echo "Invalid option: -$OPTARG"   
  16.       ;;  
  17.   esac  
  18. done  
  19. [hello@Git shell]$   

情况二,有冒号:
  1. [hello@Git shell]$ bash test.sh -a hello  
  2. this is -a the arg is ! hello  
  3. [hello@Git shell]$ bash test.sh -a   
  4. [hello@Git shell]$ more test.sh   
  5. #!/bin/bash  
  6.    
  7. while getopts ":a:" opt; do  
  8.   case $opt in  
  9.     a)  
  10.       echo "this is -a the arg is ! $OPTARG"   
  11.       ;;  
  12.     \?)  
  13.       echo "Invalid option: -$OPTARG"   
  14.       ;;  
  15.   esac  
  16. 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。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值