How to make case statement match a number range?

后端 未结 2 1586
终归单人心
终归单人心 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 07:59

    if and [ solution

    arg=1
    if [ "$arg" -lt 5 ]; then
      echo 'less than 5'
    elif [ "$arg" -lt 15 ]; then
      echo 'less than 15'
    elif [ "$arg" -eq 17 ] || [ "$arg" -eq 19 ]; then
      echo '17 or 19'
    else
      echo 'neither'
    fi
    

    POSIX 7

    Bash follows POSIX as mentioned by https://stackoverflow.com/a/25482040/895245

    Here is the quote: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_01 section "Case Conditional Construct":

    The conditional construct case shall execute the compound-list corresponding to the first one of several patterns (see Pattern Matching Notation) [...] Multiple patterns with the same compound-list shall be delimited by the '|' symbol. [...]

    The format for the case construct is as follows:

    case word in
         [(] pattern1 ) compound-list ;;
         [[(] pattern[ | pattern] ... ) compound-list ;;] ...
         [[(] pattern[ | pattern] ... ) compound-list]
      esac
    

    and then http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13 section "2.13. Pattern Matching Notation" says:

    [ If an open bracket introduces a bracket expression as in XBD RE Bracket Expression

    and extended regular expressions points to section "9.3.5 RE Bracket Expression" which says:

    A bracket expression (an expression enclosed in square brackets, "[]" ) is an RE that shall match a specific set of single characters, and may match a specific set of multi-character collating elements, based on the non-empty set of list expressions contained in the bracket expression.

    So only individual characters are considered when you do something like:

    [9-10]
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题