How to make case statement match a number range?

后端 未结 2 1591
终归单人心
终归单人心 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]
    

提交回复
热议问题