getopt getopts
来源:互联网 发布:centos 安装opera 编辑:程序博客网 时间:2024/06/05 14:13
一.getopt
1.使用
getopt optstring options parameters
optstring定义了命令行有效的选项字母,还定义了哪些选项字需要参数,需要参数的选项后面加:即可;
如果指定了一个不在optstring中的选项,默认情况下,getopt命令会产生一条错误消息,如果想要忽略,可以加上-q选项;
getopt ab:cd -a -b test1 -cd test2 test3:
定义了4个有效选项,还指定b需要参数值,它会自动将-cd选项分成两个单独的选项,并插入双破折号来分开行中的额外参数;
二.getopts
1.使用
getopts optstrings variable
每次调用它时,它只处理一个命令行上检测到的参数,处理完所有参数后,它会退出并返回一个大于0的退出状态码。
如果选项需要跟一个参数,则在optstrings后跟一个:即可,要去掉错误消息的话,在optstrings前加:,getopts命令将当前参数保存在命令行中定义的variable中;
getopts有两个内置变量,OPTARG:保存选项后的参数值,OPTIND:保存了参数列表中正在处理的参数位置;
getopts命令解析选项时,会移除开头的-(单破折号);
选项和参数值连在一起时,getopts也能分别识别它们,所以使用时选项和参数直接可以不用加空格;
getopts能将命令行上未定义的选项统一输出成问号。
while getopts :ab:c opt
do
case "$opt" in
a)cmd1;;
b)cmd2;with value $OPTARG;;
c)cmd3;
*);;
esac
done
- getopt getopts
- getopts/getopt(转载)
- C getopt 及 shell getopts
- shell中的getopt与getopts
- getopts 和 getopt 用法简记
- Shell 处理命令行参数 getopts 和 getopt
- Shell中使用getopt、getopts命令
- shell 脚本中getopt 和 getopts 的区别
- Shell的getopts和C语言的getopt对比
- shell中getopt/getopts的使用 getarg和getind意义
- getopt/getopts:Bash中命令行选项/参数处理
- 格式化命令行参数getopt和getopts的使用
- shell脚本选项参数处理学习getopts|getopt
- getopts
- getopts
- getopts
- linux bash shell 中getopts 命令 和 python 中 getopt 函数的比较总结
- getopt
- twemproxy源码分析之二 nc_run
- 提高效率—编程中的技巧
- jsp 生成验证码代码
- Spring中的事务控制(Transacion Management with Spring)
- Google I/O 2015 为 Android 开发者带来了哪些福利?
- getopt getopts
- 【PHP学习】函数
- Twemproxy源码分析之三:其进程以及时间模型
- ndk-lib实现编译并将生成文件拷贝到项目libs中
- 推荐一本Android开发入门书籍
- Android学习之SharePreferences轻量级数据存储
- 开始刷leedtcode day72:Product of Array Except Self
- 周一工作
- [高可用]Ubuntu搭建高可用的MariaDB Galera数据库集群