How to make case statement match a number range?

BashSwitch Statement

Bash Problem Overview


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 code:

    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 expect this code to output may be ok but get no clue - 10.

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

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]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshamikView Question on Stackoverflow
Solution 1 - BashArnon ZilcaView Answer on Stackoverflow
Solution 2 - BashCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow