[记录]Shell中的getopts和getopt用法
Shell中的getopts和getopt用法
1.getopts
getopts(shell内置命令)不能直接处理长的选项(如:--prefix=/home等),getopts有两个参数,第一个参数是一个字符串,包括字符和“:”,每一个字符都是一个有效的选项,如果字符后面带有“:”,表示这个字符有自己的参数。getopts从命令中获取这些参数,并且删去了“-”,并将其赋值在第二个参数中,如果带有自己参数,这个参数赋值在“optarg”中。提供getopts的shell内置了这里变量$optarg存储相应选项的参数,而$optind总是存储原始$*中下一个要处理的元素位置。while getopts ":a:bc" opt #第一个冒号表示忽略错误;字符后面的冒号表示该选项必须有自己的参数.
例子:
#cat getopts.sh
#!/bin/bash echo $*
while getopts ":a:bc" opt
do
case $opt in
a )
echo $optarg
echo $optind
;;
b )
echo "b $optind"
;;
c )
echo "c $optind"
;;
? )
echo "error"
exit 1
;;
esac
done
echo $optind
shift $(($optind - 1))
#通过shift $(($optind - 1))的处理,$*中就只保留了除去选项内容的参数,可以在其后进行正常的shell编程处理了。
echo $0
echo $*
执行命令:
./getopts.sh -a 11 -b -c
-a 11 -b -c
11
3
b 4
c 5
5
./getopts.sh 2.getopt
getopt(一个外部工具)具体用用法可以#man getopt
#-o表示短选项,两个冒号表示该选项有一个可选参数,可选参数必须紧贴选项,如-carg 而不能是-c arg
#--long表示长选项
例子:
#cat getopt.sh
#!/bin/bash # a small example program for using the new getopt(1) program.
# this program will only work with bash(1)
# an similar program using the tcsh(1) script. language can be found
# as parse.tcsh
# example input and output (from the bash prompt):
# ./parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long "
# option a
# option c, no argument
# option c, argument `more'
# option b, argument ` very long '
# remaining arguments:
# --> `par1'
# --> `another arg'
# --> `wow!*\?'
# note that we use `"$@"' to let each command-line parameter expand to a
# separate word. the quotes around `$@' are essential!
# we need temp as the `eval set --' would nuke the return value of getopt.
#-o表示短选项,两个冒号表示该选项有一个可选参数,可选参数必须紧贴选项
#如-carg 而不能是-c arg
#--long表示长选项
#"$@"在上面解释过
# -n:出错时的信息
# -- :举一个例子比较好理解:
#我们要创建一个名字为 "-f"的目录你会怎么办?
# mkdir -f #不成功,因为-f会被mkdir当作选项来解析,这时就可以使用
# mkdir -- -f 这样-f就不会被作为选项。
temp=`getopt -o ab:c:: --long a-long,b-long:,c-long:: \
-n 'example.bash' -- "$@"`
if [ $? != 0 ] ; then
echo "terminating..." >&2
exit 1
fi
# note the quotes around `$temp': they are essential!
#会将符合getopt参数规则的参数摆在前面,其他摆在后面,并在最后面添加--
eval set -- "$temp"
#经过getopt的处理,下面处理具体选项。
while true
do
case "$1" in
-a|--a-long)
echo "option a"
shift
;;
-b|--b-long)
echo "option b, argument \`$2'"
shift 2
;;
-c|--c-long)
case "$2" in
"")
echo "option c, no argument"
shift 2
;;
*)
echo "option c, argument \`$2'"
shift 2
;;
esac ;;
--)
shift
break
;;
*)
echo "internal error!"
exit 1
;;
esac
done
echo "remaining arguments:"
for arg do
echo '--> '"\`$arg'" ;
done
运行命令:
./getopt.sh --b-long abc -a -c33 remain
option b, argument `abc'
option a
option c, argument `33'
remaining arguments:
--> `remain'
[记录]Shell中的getopts和getopt用法的更多相关文章
- Shell中的(),{}几种语法用法-单独总结
shell中的(),{}几种语法用法 查看脚本语法是否有错误: bash -n modify_suffix.sh 跟踪执行 sh -x modify_suffix.sh aaa 1. ${var} 2 ...
- shell中的(),{}几种语法用法
转自:https://www.cnblogs.com/HKUI/p/6423918.html 查看脚本语法是否有错误:bash -n modify_suffix.sh跟踪执行sh -x modify_ ...
- Shell中三种引号的用法及区别
Linux Shell中有三种引号,分别为双引号(" ").单引号(' ')以及反引号(` `). 其中双引号对字符串中出现的$.''.`和\进行替换:单引号不进行替换,将字符串中 ...
- Linux shell中&,&&,|,||的用法
前言 在玩dvwa的命令注入漏洞的时候,遇到了没有预料到的错误,执行 ping 127.0.0.1 & echo "<?php phpinfo(); ?>" & ...
- 发现linux shell中$0,$?,$!等的特殊用法
记录下linux shell下的特殊用法及参数的说明 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行的命令的结束代 ...
- linux shell中$0,$?,$!等的特殊用法
记录下linux shell下的特殊用法及参数的说明 变量说明: $$Shell本身的PID(ProcessID)$!Shell最后运行的后台Process的PID$?最后运行的命令的结束代码(返回值 ...
- shell中echo基础及高级用法详解-渐入佳境
--作者:飞翔的小胖猪 --创建时间:2021年2月19日 1.1 基础用法 echo命令用来输出文本,在shell脚本中用来输出提示信息用的比较多. 单引号:原样输出所有的内容,不用转义就能输出特殊 ...
- Shell中read的选项及用法
1. Read的一些选项 Read可以带有-a, -d, -e, -n, -p, -r, -t, 和 -s八个选项. -a :将内容读入到数值中 echo -n "Input muliple ...
- shell中$0,$?,$!等的特殊用法
变量说明: $$Shell本身的PID(ProcessID)$!Shell最后运行的后台Process的PID$?最后运行的命令的结束代码(返回值)$-使用Set命令设定的Flag一览$*所有参数列表 ...
随机推荐
- eclipse导入项目之后报错
一.项目本身就有错 二.jdk版本的问题 参考网址:http://jingyan.baidu.com/article/95c9d20da3ec5fec4e756186.html 从别的地方导入一个项目 ...
- aliyun 购买的linux安装tomcat
按照网上的教程,下载tomcat,解压(即安装),启动,发现无法访问.有说端口未开放,修改/etc/sysconfig/iptables,添加端口开放.未发现有此文件,只有iptables-confg ...
- Eclipse运行Java简单实例
运行eclipse前首先配置好JDK环境变量等 双击这句话可跳转配置环境变量详细步骤 运行eclipse软件 1.File菜单-New - project 2.Java Project - Next ...
- java对象引用-要掌握的细节2
目的:加深对java引用变量的理解,避免数据的处理的偏差. 问题: 1.一个集合List ,将新建Student对象stu放入List之后,再给stu的成员变量例如id,name赋值.此时List中的 ...
- 性能测试监控:Jmeter +InfluxDB +collectd +Grafana
虚拟机ip 192.168.180.128 Influxdb Influxdb是一个开源的分布式时序.时间和指标数据库,使用go语言编写,无需外部依赖. 它有三大特性: 时序性(Time Series ...
- SpringMVC环境搭建---xml版及注解版
一.建立 JavaWeb 项目(基于Intellij 14.0.3搭建) 1.建立一个 Java 项目,在项目下新建一个文件夹 webapp ,然后在该文件夹下新建一个 WEB-INF 文件夹: 2. ...
- 15_Python函数名本质
函数名的本质 函数名实质上就是函数的内存地址 def wrapper(): pass print(wrapper) 1.引用是什么? 当我们定义a=1的时候,系统会开辟一块内存空间来保存1,然后用a变 ...
- cdh版本的hive安装以及配置
hive依赖hadoop 需要的软件包:hive-0.13.1-cdh5.3.6.tar.gz .hadoop-2.5.0-cdh5.3.6.tar.gz 1.hadoop的安装步骤请访问: http ...
- Effective Java 第三版——31.使用限定通配符来增加API的灵活性
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- python 3.x 爬虫基础---http headers详解
前言 上一篇文章 python 爬虫入门案例----爬取某站上海租房图片 中有对headers的讲解,可能是对爬虫了解的不够深刻,所以老觉得这是一项特别简单的技术,也可能是简单所以网上对爬虫系统的文档 ...