免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4381 | 回复: 8
打印 上一主题 下一主题

shell中getopt问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-07 11:46 |只看该作者 |倒序浏览
最近写脚本的时候遇到要使用getopt来检查命令行参数是否合法,下面是我的脚本

  1. #!/bin/bash


  2. ARGS=`getopt -o ab:c:: -l a-long,b-long:,c-long:: -- "$@"`


  3. [ $? -ne 0 ] && usage  
复制代码
可是在调用的时候我使用./test.sh --alon的时候它并没有提示错误,我用./test.sh --a-long-t的时候就会提示错误!
是不是getopt在解析的时候匹配不会精确匹配,有没有什么办法让它在./test.sh --alon的时候提示不认识这个选项?

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
2 [报告]
发表于 2012-03-07 12:05 |只看该作者
getopt 不支持长选项

论坛徽章:
0
3 [报告]
发表于 2012-03-07 12:48 |只看该作者
支持的吧, -l a-long,b-long:,c-long:: 这个就是常选项吧

论坛徽章:
0
4 [报告]
发表于 2012-03-07 13:04 |只看该作者
victorplus 发表于 2012-03-07 12:48
支持的吧, -l a-long,b-long:,c-long:: 这个就是常选项吧

你要先明白什么是“长选项”

论坛徽章:
0
5 [报告]
发表于 2012-03-07 13:46 |只看该作者
xiaopan3322 发表于 2012-03-07 13:04
你要先明白什么是“长选项”


       -l, --longoptions longopts
              The long (multi-character) options to be recognized.  More than one option name may be specified at once, by separat‐
              ing the names with commas. This option may be given more than once, the longopts are cumulative.   Each  long  option
              name  in  longopts may be followed by one colon to indicate it has a required argument, and by two colons to indicate
              it has an optional argument.

这个是man手册上面写的!

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
6 [报告]
发表于 2012-03-07 14:37 |只看该作者
请注意你引用man手册上的两个词: option和argument
区别一下吧

论坛徽章:
0
7 [报告]
发表于 2012-03-07 14:47 |只看该作者
waker 发表于 2012-03-07 14:37
请注意你引用man手册上的两个词: option和argument
区别一下吧

首先,我要声明一下,我这个说的getopt不是下面的这个:
       int getopt(int argc, char * const argv[],
                  const char *optstring);
而是

GETOPT(1)                                                                                                                 GETOPT(1)

NAME
       getopt - parse command options (enhanced)
这个!
现在我的需求是在shell脚本中用getopt来检查一下命令行选项以及选项参数传递是否正确!
例如,shell脚本支持:
test.sh --version|-V
test.sh --update {a | b}
当我输入test.sh --up的时候它应该提示不认识这个选项而不是缺少选项参数,而当我输入./test.sh --update,它应该提示我缺少选项参数!

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
8 [报告]
发表于 2012-03-07 14:48 |只看该作者
victorplus 发表于 2012-03-07 14:47
首先,我要声明一下,我这个说的getopt不是下面的这个:
       int getopt(int argc, char * const arg ...


getopt 不支持长选项

论坛徽章:
4
白羊座
日期:2014-10-10 18:07:34黑曼巴
日期:2017-12-04 19:02:3520周年集字徽章-20	
日期:2020-10-28 14:04:3020周年集字徽章-周	
日期:2020-10-28 14:13:30
9 [报告]
发表于 2014-07-29 14:03 |只看该作者
[ $? -ne 0 ] && usage  的usage 起什么作用?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP