如何使case语句匹配一个数字范围? [英] How to make case statement match a number range?

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

问题描述

我正在运行一个列号范围为 0 - 50 的 switch case.现在每个 case 都支持离散列号,我观察到它的失败.

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

我希望这段代码能够输出 maybeok 但得到 no crowd - 10.

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

推荐答案

Bash case 不适用于数字范围.[] 用于 shell 模式.

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 可以是 real 介于 9 和 10 之间,那么您需要使用 if(而不是大小写)与范围.

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

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

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