如何使案例陈述与数字范围匹配? [英] How to make case statement match a number range?

查看:52
本文介绍了如何使案例陈述与数字范围匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个开关箱,其列号可以在0到50的范围内.现在每种情况都支持离散的列号,并且观察到它的故障.

I'm running a switch case with column numbers which can be in the range 0 - 50. Now each case supports discrete column number and I observe its failure.

这是代码:

    i=10
    a=1
    b=0.65
    if [ "$a" != "$b" ]; then 
       case $i in  
            [1]|[2]|[5]) echo "Not OK"; ;; 
            [9-10]|[12]) echo "may be ok"; ;; 
            *) echo "no clue - $i"; ;; 
       esac
   fi

我希望此代码输出可能没问题,但毫无头绪-10 .

I expect this code to output may be ok but get no clue - 10.

推荐答案

重击 case 不适用于数字范围. [] 用于外壳模式.

Bash case doesn't work with numbers ranges. [] is for shell patterns.

例如,在这种情况下, [1-3] 5 | 6)适用于15或25或35或6.

for instance this case [1-3]5|6) will work for 15 or 25 or 35 or 6.

您的代码应如下所示:

i=10
a=1
b=0.65
if [ "$a" != "$b" ] ; then
   case $i in
        1|2|5) echo "Not OK"; ;;
        9|10|12) echo "may be ok"; ;;
        *) echo "no clue - $i"; ;;
   esac;
fi

如果 i 可以是9到10之间的 real ,那么您需要使用 if (而不是情况).

If i can be real between 9 and 10 then you'll need to use if (instead of case) with ranges.

这篇关于如何使案例陈述与数字范围匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆