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用法的更多相关文章

  1. Shell中的(),{}几种语法用法-单独总结

    shell中的(),{}几种语法用法 查看脚本语法是否有错误: bash -n modify_suffix.sh 跟踪执行 sh -x modify_suffix.sh aaa 1. ${var} 2 ...

  2. shell中的(),{}几种语法用法

    转自:https://www.cnblogs.com/HKUI/p/6423918.html 查看脚本语法是否有错误:bash -n modify_suffix.sh跟踪执行sh -x modify_ ...

  3. Shell中三种引号的用法及区别

    Linux Shell中有三种引号,分别为双引号(" ").单引号(' ')以及反引号(` `). 其中双引号对字符串中出现的$.''.`和\进行替换:单引号不进行替换,将字符串中 ...

  4. Linux shell中&,&&,|,||的用法

    前言 在玩dvwa的命令注入漏洞的时候,遇到了没有预料到的错误,执行 ping 127.0.0.1 & echo "<?php phpinfo(); ?>" & ...

  5. 发现linux shell中$0,$?,$!等的特殊用法

    记录下linux shell下的特殊用法及参数的说明 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行的命令的结束代 ...

  6. linux shell中$0,$?,$!等的特殊用法

    记录下linux shell下的特殊用法及参数的说明 变量说明: $$Shell本身的PID(ProcessID)$!Shell最后运行的后台Process的PID$?最后运行的命令的结束代码(返回值 ...

  7. shell中echo基础及高级用法详解-渐入佳境

    --作者:飞翔的小胖猪 --创建时间:2021年2月19日 1.1 基础用法 echo命令用来输出文本,在shell脚本中用来输出提示信息用的比较多. 单引号:原样输出所有的内容,不用转义就能输出特殊 ...

  8. Shell中read的选项及用法

    1. Read的一些选项 Read可以带有-a, -d, -e, -n, -p, -r, -t, 和 -s八个选项. -a :将内容读入到数值中 echo -n "Input muliple ...

  9. shell中$0,$?,$!等的特殊用法

    变量说明: $$Shell本身的PID(ProcessID)$!Shell最后运行的后台Process的PID$?最后运行的命令的结束代码(返回值)$-使用Set命令设定的Flag一览$*所有参数列表 ...

随机推荐

  1. Java中的对象Object方法之---wait()和notifiy()

    这一篇咋们继续,接着来介绍wait()和notify()方法,我们都知道这两个方法和之前介绍的方法不太一样,那就是这两个方法是对象Object上的,不属于Thread类上的.我们也知道这两个方法是实现 ...

  2. Java - 路线图

    java语言基础 基本语法 面向对象思想 mysql数据库基础 jdbc操作 java高级技术 java集合框架 多线程 网络编程 sql深入,索引,sql优化 javaweb servlet jsp ...

  3. svn checkout The XML response contains invalid XML

    svn checkout 报错:The XML response contains invalid XML 待解决? ---目前没有找到好的解决方法,svn数据库中存的log入手应该可以,有时间再去看 ...

  4. 红米 Note3 (kenzo)刷入 Lineage OS

    红米 Note3 (kenzo)刷入Lineage OS 的过程 详细的教程请看 Install LineageOS on kenzo 本文做一些额外的补充 0 刷机之的准备工作 如果登陆了 Goog ...

  5. Mysql的主从配置

    前言:这次学习分布式的思想要配置mysql的主从复制和读写分离,我在主从配置上踩到很多坑,在此演示一遍配置过程,并附上问题的说明和自己的一些见解 Mysql主从复制的原理 附上原理图: mysql的主 ...

  6. Redis的部署

    笔者Q:972581034 交流群:605799367 欢迎加群交流 官方网站 redis.io 下载 cd /usr/local/src wget http://download.redis.io/ ...

  7. Div+Css画太极图源代码

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>D ...

  8. nginx加权轮询和ip_hash

    nginx为后端web服务器(apache,nginx,tomcat,weblogic)等做反向代理 几台后端web服务器需要考虑文件共享,数据库共享,session共享问题.文件共享可以使用nfs, ...

  9. TCP/IP参考模型

    1.简介 什么是TCP/IP参考模型? TCP/IP模型是网络通信模型的一种.网络通信模型还包括OSI,旨在使各种计算机在世界范围内互连为网络.其中有OSI为七层模型.TCP/IP为四层模型,现在大部 ...

  10. python _init_学习

    今天继续学习python,接触了_init_,感觉很好玩照着教程手写了一些代码,感觉编程语言是互通的,只是换个了形式来表达 #coding=utf-8#类似于java的构造器class Person: ...