Bash shell编程<七>:处理选项、getopt命令
2016-01-14 12:35
671 查看
找出选项
处理简单选项使用shift和case来处理
#/bin/bash while [ -n $1 ] do case "$1" in -a) echo "found tha -a option";; -b) echo "found tha -b option";; -c) echo "found tha -c option";; *) echo "$1 is not a option";; esac shift done
这样运行测试
./test15 -a -b -c -d
从参数中分离选项
执行脚本时,经常会遇到既需要使用选项油需要使用参数的情况。在Linux中标准方式是通过特殊字符码将二者分开,这个特殊字符码告诉脚本选项结束和普通参数开始的位置。对于Linux来说,这个特殊的符号就是–。shell使用双破折号指示选项列表的结束,发现双破折号后,脚本能够安全的将剩余的命令行参数作为参数而不是选项处理
要想检查双破折号,只需在case语句中增加一个条目即可
#/bin/bash while [ -n $1 ] do case "$1" in -a) echo "found tha -a option";; -b) echo "found tha -b option";; -c) echo "found tha -c option";; --) shift break;; *) echo "$1 is not a option";; esac shift done count=1 for para in $@ do echo "para #$count : $para" count=$[ $count + 1 ] done
这个脚本使用break命令使得遇到双破折号从while循环中跳出,因为提前跳出,所以需要保证加入一个shift命令将双破折号从参数变量中丢弃
第一个测试
./test16 -c -a -b test1 test2
第二个测试
./test16 -c -a -b -- test1 test2
处理带值的选项
有的选项需要附加的参数值
下面看一个栗子
使用getopt命令
getopt命令处理命令行选项和参数时非常方便,它对命令行参数进行重新组织,使其更便于在脚本中解析- 命令格式
如下
getopt options optstring parameters
选项字符串(opstring)是处理的关键,它定义命令行中的有效选项字母。它还定义哪些选项字母需要参数值。
首先在选项字符串中列出将在脚本中用到的每个命令行的有效选项字母。然后在每个需要参数值的选项字母后面防止一个冒号,如
getopt ab:cd -a -b test1 -cd test2 test3 #解析为: -a -b test1 -c -d -- test2 test3 #其中的选项字符串定义了4个有效选项字母,a,b,c,d还定义选型字母b需要一个参数值,当执行getopt命令时,会监测提供的参数列表,然后提供的选项字符串对列表进行解析,注意解析时,自动将 -cd分割为两个不同的选项,并插入双破折号来分割行中的额外参数
如果指定的选项不包含在选项字符串中,getopt命令会默认生成一个错误消息。
getopt ab:cd -a -b test1 -cde test2 test3 #getopt:无效选项 -- e -a -b test1 -c -d -- test2 test3
如果要忽略这个错误消息,可以在getopt中使用-q选项,注意getopt命令选项必须在选项字符串的前面
- 在脚本中使用getopt
可以在脚本中使用getopt命令格式化为脚本输入的任意命令选项或参数,这里得将现有的命令行选项和参数替换为getopt命令生成的格式化形式,方法是使用set命令。
set命令的一个选项是双破折号,表示将命令行参数变量替换为set命令行中的值,如下
set -- `getopts -q ab:cd "$@"`
解释:将原始命令行参数送给getopt命令,然后将getopt命令中的输出送出给set命令,以遍将原始命令行参数替换为通过getopt格式化过的更精细的形式。现在原始的命令行参数替换为getopt命令的输出,getopt命令将命令行参数进行格式化。
栗子
#!/bin/bash set -- `getopt -q ab:c "$@"` while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option";; -b) param="$2" echo "Found the -b option with the param $param" shift;; -c) echo "Found -c option";; --)shift break;; *) echo "$1 is not an option";; esac shift done count=1 for param in "$@" do echo "param #$count : $param" count=$[ $count + 1 ] done
测试
./multem -ac #输出 Found the -a option Found -c option ./multem -a -b test1 -cd test2 test3 test4 #输出 Found the -a option Found the -b option with the param 'test1' Found -c option param #1 : 'test2' param #2 : 'test3' param #3 : 'test4'
但有个小bug,看下面测试
./multem -a -b test1 -cd "test2 test3" test4 #输出 Found the -a option Found the -b option with the param 'test1' Found -c option param #1 : 'test2 param #2 : test3' param #3 : 'test4'
getopt命令不能很好的处理带有空格的参数值,它将空格解析为参数分割符,而不是将双引号引起来的两个值合并为一个参数。
相关文章推荐
- Linux 开机启动[root@localhost]变成bash-3.2#解决方法
- shell脚本编程之补充知识点
- shell脚本编程之数组
- #!/bin/bash
- Shell脚本编程之判断语句
- Linux SHELL 命令入门题目答案(一)
- Linux SHELL 命令入门题目(一)
- Hadoop Shell命令字典
- 开始Shell编程
- HBase shell查询
- Linux Shell 基础
- Linux Shell Script目录
- shell脚本编程之数组
- shell脚本编程之循环语句
- shell脚本编程之判断语句
- Week 1 solved question: change environment by shell
- shell脚本ping -w用法
- Shell脚本编程之判断语句
- shell脚本编程之循环语句
- shell脚本编程之数组