getopts
获取UNIX类型的选项:
unix有一个优点就是标准UNIX命令在执行时都具有相同的命令行格式:
command -options parameters
如果在执行Shell程序也采用上述格式,Bourne Shell中提供了一条获取和处理命令行选项的语句,即getopts语句。该语句的格式为:
getopts option_string variable
其中option_string中包含一个有效的单字符选项。若getopts命令在命令行中发现了连字符,那么它将用连字符后面的字符同 option_string相比较。若有匹配,则把变量variable的值设为该选项。若无匹配,则variable设为?。当getopts发现连字 符后面没有字符,会返回一个非零的状态值。Shell程序中可以利用getopts的返回值建立一个循环。
有时侯选项中还带一个值,getopts命令同样也支持这一功能。这时需要在option_string中选项字母后加一个冒号。当 getopts命令发现冒号后,会从命令行该选项后读取该值。若该值存在,那么将被存在一个特殊的变量OPTARG中。如果该值不存在,getopts命 令将在OPTARG中存放一个问号,并且在标准错误输出上显示一条消息。
optstring option字符串,会逐个匹配
varname 每次匹配成功的选项
arg 参数列表,没写时它会取命令行参数列表
$OPTIND 特殊变量,option index,会逐个递增, 初始值为1
$OPTARG 特殊变量,option argument,不同情况下有不同的值
细则1:当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。
细则2:当optstring中的字母跟”:“时,表明该option可接参数,参数(argument)放在$OPTARG中;
如果缺参数,且optstring是以”:“开头,则varname的值会是:,$OPTARG是该option,
否则varname的值是?,$OPTARG是该option。(参照细则1)
getopts options variable
getopts 的设计目标是在循环中运行,每次执行循环,getopts 就检查下一个命令行参数,并判断它是否合法。即检查参数是否以 - 开头,后面跟一个包含在 options 中的字母。如果是,就把匹配的选项字母存在指定的变量 variable 中,并返回退出状态0;如果 - 后面的字母没有包含在 options 中,就在 variable 中存入一个 ?,并返回退出状态0;如果命令行中已经没有参数,或者下一个参数不以 - 开头,就返回不为0的退出状态。
二、使用举例
cat args #!/bin/bash
while getopts h:ms option
do
case "$option" in
h)
echo "option:h, value $OPTARG"
echo "next arg index:$OPTIND";;
m)
echo "option:m"
echo "next arg index:$OPTIND";;
s)
echo "option:s"
echo "next arg index:$OPTIND";;
\?)
echo "Usage: args [-h n] [-m] [-s]"
echo "-h means hours"
echo "-m means minutes"
echo "-s means seconds"
exit 1;;
esac
done echo "*** do something now ***"
./args -h 100 -ms option:h, value 100
next arg index:3
option:m
next arg index:3
option:s
next arg index:4
*** do something now ***
./args -t
./args: illegal option -- t
Usage: args [-h n] [-m] [-s]
-h means hours
-m means minutes
-s means seconds
getopts的更多相关文章
- 使用getopts处理shell中的输入参数
在编写shell脚本中,经常要处理一些输入参数,在使用过程中发现getopts更加方便,能够很好的处理用户输入的参数和参数值. getopts用于处理用户输入参数,举例说明使用方法: while ...
- shell命令getopts解析
getopts是一条获取和处理命令行选项的语句,格式为getopts option_string variable .其中option_string中包含一个有效的单字符选项,若getopts命令在命 ...
- linux命令getopts
一.getopts 简介 由于shell命令行的灵活性,自己编写代码判断时,复杂度会比较高.使用内部命令 getopts 可以很方便地处理命令行参数.一般格式为: getopts options va ...
- 过生日,也要学学哈,这次是SHELL的GETOPTS
今天是WEBSOCKET,, 先完成一个SHELL的GETOPS,周一就用得着. #!/bin/bash echo "usage: ./$0 -t (prism|opscripts)&quo ...
- getopt vs getopts
getopt示例 #!/bin/bash aflag=no args=`getopt a: $@` ]; then echo 'Usage: ...' exit fi set -- $args ] d ...
- [记录]Shell中的getopts和getopt用法
Shell中的getopts和getopt用法 1.getopts getopts(shell内置命令)不能直接处理长的选项(如:--prefix=/home等),getopts有两个参数,第一个参数 ...
- getopts的使用
getopts的使用 语法格式:getopts [option[:]] [DESCPRITION] VARIABLE option:表示为某个脚本可以使用的选项 ":":如果某个选 ...
- shell脚本中关于getopts的使用方法
例子: while getopts ":e:s:pd:l" arg; do case $arg in e) ghs_env=$OPTARG ;; s) ghs_service=$O ...
- Linux getopt/getopts解析命令行参数教程
一.说明 shell中获取参数可以直接使用$1.$2等形式来获取,但这种方式有明显的限制:每个参数的位置是固定的.比如如果在设计上$1是ip地址$2是端口,那在执行时就必须第一个参数是ip第二个参数是 ...
随机推荐
- Keepalived 安装与配置
下载:http://www.keepalived.org/ what is keepalived? Keepalived is a routing software written in C. The ...
- iOS中scrollview是否要回弹
1. @property(nonatomic) BOOL bounces //当滚动到内容边缘是否发生反弹,default is YES.2. @property(nonatomic) BOOL al ...
- Lintcode: Update Bits
Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits be ...
- JAVA-封装-静态属性
1.使用 1.static 2.用来修饰属性.方法.内部类.代码块 3.称为类属性,静态属性,类方法,静态方法 3.不需要实例化,直接用类名或静态成员名调用 2.特点 1.静态属性对于类的所有实例是共 ...
- [原创] 关于quartz (spring 中的任务调度器)时间配置
1. CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 ...
- C++之路进阶——bzoj1030(文本生成器)
F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser hyxzc Logout 捐赠本站 Notice:由于本OJ建立在 ...
- 数据库中is null(is not null)与=null(!=null)的区别
在标准SQL语言(ANIS SQL)SQL-92规定的Null值的比较取值结果都为False,既Null=Null取值也是False.NULL在这里是一种未知值,千变万化的变量,不是“空”这一个定值! ...
- geotools
http://blog.tigerlihao.cn/2010/01/geotools-based-web-map-service.html
- js this 闭包
var myObject = { value :, increment:function (inc){ ; } }; myObject .increment(); console.log(myObje ...
- sql 存储过程 分页
ALTER PROCEDURE [dbo].[BrokerToLenderDataShow2]@Where VARCHAR(200), --查询条件 不含'where'字符,如id>10 and ...