How to make case statement match a number range?

后端 未结 2 1609
终归单人心
终归单人心 2021-01-31 07:23

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.

Here is the cod

2条回答
  •  情深已故
    2021-01-31 08:01

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

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

    Your code should look like this:

    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
    

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

提交回复
热议问题