Bash shell编程<七>:处理选项、getopt命令
来源:互联网 发布:团员考试答案软件 编辑:程序博客网 时间:2024/06/16 04:40
找出选项
- 处理简单选项
使用shift和case来处理
#/bin/bashwhile [ -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 shiftdone
这样运行测试
./test15 -a -b -c -d
- 从参数中分离选项
执行脚本时,经常会遇到既需要使用选项油需要使用参数的情况。在Linux中标准方式是通过特殊字符码将二者分开,这个特殊字符码告诉脚本选项结束和普通参数开始的位置。对于Linux来说,这个特殊的符号就是–。shell使用双破折号指示选项列表的结束,发现双破折号后,脚本能够安全的将剩余的命令行参数作为参数而不是选项处理
要想检查双破折号,只需在case语句中增加一个条目即可
#/bin/bashwhile [ -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 shiftdonecount=1for 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/bashset -- `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 shiftdonecount=1for param in "$@"do echo "param #$count : $param" count=$[ $count + 1 ]done
测试
./multem -ac#输出Found the -a optionFound -c option./multem -a -b test1 -cd test2 test3 test4#输出Found the -a optionFound the -b option with the param 'test1'Found -c optionparam #1 : 'test2'param #2 : 'test3'param #3 : 'test4'
但有个小bug,看下面测试
./multem -a -b test1 -cd "test2 test3" test4#输出Found the -a optionFound the -b option with the param 'test1'Found -c optionparam #1 : 'test2param #2 : test3'param #3 : 'test4'
getopt命令不能很好的处理带有空格的参数值,它将空格解析为参数分割符,而不是将双引号引起来的两个值合并为一个参数。
0 0
- Bash shell编程<七>:处理选项、getopt命令
- getopt/getopts:Bash中命令行选项/参数处理
- Bash Shell 命令行 选项/参数处理
- Bash Shell中命令行选项/参数处理
- Bash Shell中命令行选项/参数处理
- Bash Shell中命令行选项/参数处理
- Bash Shell中命令行选项/参数处理
- Bash Shell中命令行选项/参数处理
- Bash Shell中命令行选项/参数处理
- Bash Shell中命令行选项/参数处理
- Bash Shell中命令行选项/参数处理
- Bash Shell中命令行选项/参数处理
- Bash Shell中命令行选项/参数处理
- Bash Shell中命令行选项/参数处理
- shell脚本选项参数处理学习getopts|getopt
- 使用getopt命令解析shell脚本的命令行选项
- 使用getopt命令解析shell脚本的命令行选项
- 使用getopt命令解析shell脚本的命令行选项
- ubuntu下搭建tomcat服务器
- Android 音乐信息乱码
- JavaWeb监听器
- Eclipse 中编辑XML文件不能进行提示的解决方法
- ssh无密码登录
- Bash shell编程<七>:处理选项、getopt命令
- CSS样式重置
- JSPatch – 动态更新iOS APP
- 操作系统概念阅读笔记3
- linux常用命令—— 系统管理(五)
- 问题集
- Tomcat 同一服务器上 不同web项目共享session的
- 关于unity3dGUI(uGUI)的一些自适应的收获,在这里跟大家分享一下
- android 打jar包