免费注册 查看新帖 |

Chinaunix

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

case 语句中的 \?) 选项是什么意思? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-01-10 16:42 |只看该作者 |倒序浏览
看《learning the bash shell》的一段代码
  1. while getopts ":ab:c" opt
  2. do
  3.         case $opt in
  4.                 a)
  5.                         echo "a";;
  6.                 b)
  7.                         echo "b $OPTARG";;
  8.                 c)
  9.                         echo "c";;
  10.                 \?)
  11.                         echo "usage: ${0##.*/} [-a] [-b barg] [-c] args..."
  12.                         exit 1
  13.         esac
  14. done

  15. shift $(($OPTIND - 1))
复制代码
最后一个\?)是什么意思?感觉效果跟*)是一样的……

整本书搜索下来,有5处这么用的,但是没看到解释。

论坛徽章:
0
2 [报告]
发表于 2011-01-10 16:48 |只看该作者
If an invalid option is seen, getopts places ? into name and, if
              not silent, prints an  error  message  and  unsets  OPTARG.   If
              getopts  is  silent,  the  option  character  found is placed in
              OPTARG and no diagnostic message is printed.

              If a required argument is not found, and getopts is not  silent,
              a  question  mark  (?) is placed in name, OPTARG is unset, and a
              diagnostic message is printed.  If getopts  is  silent,  then  a
              colon  (  is  placed  in  name and OPTARG is set to the option
              character found.

论坛徽章:
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
3 [报告]
发表于 2011-01-10 16:50 |只看该作者
匹配一个字符:?

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
4 [报告]
发表于 2011-01-10 16:55 |只看该作者
就是匹配问号字符,
?会被shell当成是任意字符,转义一下

论坛徽章:
0
5 [报告]
发表于 2011-01-10 16:57 |只看该作者
哦,我明白了,假如传了一个它不认识的参数,比如说 -z
它会把opt设为?,而不是z
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP