免费注册 查看新帖 |

Chinaunix

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

请教为什么我的getops中的case没起作用啊 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-21 12:20 |只看该作者 |倒序浏览
高手帮帮忙,我写了个简单的getops以便学习,可是系统当我输入./getopts.sh -x -y YANG -z GE *的时候,只有for里的echo显示出数据了,为什么没有打印出xopt,yopt,zopt的值啊,请问该如何做才能解决这个问题啊,下面是程序getopts.sh(我用的是redhat9.0)
while getopts "y:z:" opt;
do
case $opt in
        X ) xopt='-x set';;
        Y ) yopt="-y set and called with $OPTARG";;
        Z ) zopt="-z set and called with $OPTARG";;
        \? ) echo 'USAGE: getopts.sh [-x] [-y arg] [-z arg ] file ...'
          exit 1
  esac
done
shift $(($OPTIND - 1))
echo ${xopt: -'did not use -x'}
echo ${yopt: -'did not use -y'}
echo ${zopt: -'did not use -z'}
echo "Remaining command-line arguments are:"
for f in "$@"
do
  echo -e "$f"
done

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2005-12-21 12:36 |只看该作者
shift那段没用吧,因为OPTIND是有自动shift的作用

论坛徽章:
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 [报告]
发表于 2005-12-21 12:44 |只看该作者
注意大小写

论坛徽章:
0
4 [报告]
发表于 2005-12-22 12:42 |只看该作者
我把后面的全都注释掉了,只留下while跟case,结果输入./getopts.sh -x -y YANG -z GE 后还是什么都没显示出来。。。
while getopts "y:z:" opt;
do
case $opt in
        X ) xopt='-x set';;
        Y ) yopt="-y set and called with $OPTARG";;
        Z ) zopt="-z set and called with $OPTARG";;
        \? ) echo 'USAGE: getopts.sh [-x] [-y arg] [-z arg ] file ...'
          exit 1
  esac
done
echo $xopt
echo $yopt
echo $zopt

论坛徽章:
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
5 [报告]
发表于 2005-12-22 12:43 |只看该作者
原帖由 waker 于 2005-12-21 12:44 发表
注意大小写

论坛徽章:
0
6 [报告]
发表于 2005-12-23 16:48 |只看该作者
大小写??是这样吗?运行结果却是 'USAGE: getopts.sh [-x] [-y arg] [-z arg ] file ...‘ 哭......如果还没改对请说详细点啊,谢谢
while getopts "y:z:" opt;
do
case $opt in
        x ) xopt='-x set';;
        y ) yopt="-y set and called with $OPTARG";;
        z ) zopt="-z set and called with $OPTARG";;
        \? ) echo 'USAGE: getopts.sh [-x] [-y arg] [-z arg ] file ...'
          exit 1
  esac
done
echo $xopt
echo $yopt
echo $zopt

[ 本帖最后由 happyhhhh2 于 2005-12-23 16:49 编辑 ]

论坛徽章:
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
7 [报告]
发表于 2005-12-23 17:07 |只看该作者
改对了,是用了除xyz以外的参数了吧?

论坛徽章:
0
8 [报告]
发表于 2005-12-23 20:41 |只看该作者
为什么你的while getopts “:”在参数前有个:呢?只有需要时间参数的时候才用:啊!
应该x:才对啊。。。并且,你的while这行的最后,没有;号吧

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
9 [报告]
发表于 2005-12-23 21:54 |只看该作者
原帖由 boss_juzi 于 2005-12-23 20:41 发表
为什么你的while getopts “:”在参数前有个:呢?只有需要时间参数的时候才用:啊!
应该x:才对啊。。。并且,你的while这行的最后,没有;号吧


             getopts  can  report errors in two ways.  If the first character
              of optstring is a colon, silent error  reporting  is  used.   In
              normal  operation  diagnostic  messages are printed when invalid
              options or missing option arguments  are  encountered.   If  the
              variable  OPTERR  is  set  to  0, no error messages will be dis-
              played, even if the first character of optstring is not a colon.

              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.

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
10 [报告]
发表于 2005-12-23 21:57 |只看该作者
原帖由 mocou 于 2005-12-23 21:54 发表


  

我还是喜欢那个写字的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP