LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1121|回复: 3

请问:case语句是否能够匹配多个选项吗?

[复制链接]
发表于 2009-1-11 12:21:27 | 显示全部楼层 |阅读模式
我编写了一段代码,主要实现从输入的命令行中提取出相关参数的功能,使用了case进行判断,源代码如下 xshift.sh:


#!/bin/sh
while [ $# -ne 0 ]
do
        case "$1" in
        -[m]?*)            # separate flags from value
                flag=`expr "$1" : '\(..\)'` #put the first two characters to the flag
                arg=`expr "$1" : '..\(.*\)'` #put the other characters to the variable arg
                echo $1
                echo $flag
                echo $arg
                shift
                set shiftdummy "$flag" "$arg" "$@"
                echo "i am in the -[m]?*)"
                ;;
        -m)     shift
                echo $1
                echo "i am in the -m)"
                MFLAGS="${MFLAGS}$1"
                ;;
        -*)
                ;;
        *)
                ;;
        esac
        shift
done

当我输入指令 ./xshift.sh -mb abc 后,输出的结果如下显示:
-mb
-m
b
i am in the -[m]?*)
b
i am in the -m)


问题出现在:我在case中进行了判断,想实现跳转到其中一个分支中去。可结果却发现,两个分支都进入了。请问这个应该怎么解释?是不是我的代码哪里有问题?
发表于 2009-1-13 13:28:01 | 显示全部楼层
缺break?
回复 支持 反对

使用道具 举报

发表于 2009-1-13 16:35:17 | 显示全部楼层
不要偷懒,一个一个写,或者把参数拆了,做个大循环,一个一个case。
回复 支持 反对

使用道具 举报

发表于 2009-1-13 19:24:23 | 显示全部楼层
在set shiftdummy "$flag" "$arg" "$@"下加echo "$@",显示当前的$@的值是shiftdummy -m b abc。不过set shiftdummy "$flag" "$arg" "$@"是什么意思啊
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表