help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: case statement with non-consecutive numbers


From: pauline-galea
Subject: Re: case statement with non-consecutive numbers
Date: Thu, 15 Apr 2021 19:13:27 +0200

> Sent: Friday, April 16, 2021 at 3:47 AM
> From: "Greg Wooledge" <greg@wooledge.org>
> To: help-bash@gnu.org
> Subject: Re: case statement with non-consecutive numbers
>
> On Thu, Apr 15, 2021 at 05:39:10PM +0200, pauline-galea@gmx.com wrote:
> > How can I deal with using a case statement with numbers which are not 
> > consecutive,
> > or with a sequence of ranges.  Looked for examples, but were not specific 
> > enough
> > about numerical values that are not consecutive.
>
> So vague.
>
> I'm going to assume you started with something like this:
>
> case $input in
>   [2-6]) echo "yeah, that's good";;
> esac
>
> and you maybe figured out how to extend it to something like this:
>
> case $input in
>   [2-68-9]) echo "yeah, that's good, screw 7";;
> esac

I struggle with what the above means, I'm afraid.

> but then you struggled with how to extend it to something like "3 to 5,
> or 9 to 13" because you can't express that with a single glob-type
> pattern.

Correct, I struggled with that.

> Fear not!  You can just use more than one pattern.
>
> case $input in
>   [3-5]|9|1[0-3]) echo "yeah, that's good";;
> esac

Means I can use "|" just as for strings.

Doing more work on this, it looks like using commas also works

      case $k in
        [1-2,4-5,7])  printf "%-21s" $s   ;;
        [3,6,8])      printf "%-21s\n" $s ;;
      esac

Would that be correct?





reply via email to

[Prev in Thread] Current Thread [Next in Thread]