2
case "$1","$name" in -py | --python | --python3,*) if [[ "$name" =~ \..+$ ]]; then 

That doesn't catch stuff, which actually it should, like…

USERNAME@HOSTNAME:~$ myscript --python surfer 

Funny thing: Simplify the multi pattern conditional to…

 --python,*) if [[ "$name" =~ \..+$ ]]; then 

and it works! With the bitterly-repetitive outlook to have to place that section 3 times: 1st for -py, then for --python, and finally for --python3 for catching all patterns.

But the other thing is - the other way around:

case "$1" in -py | --python | --python3) if [[ ! "$name" =~ \.py$ ]]; then 

That's fine, that works! So, that disproves my assumption, that the multi pattern syntax might be incorrect, might needs the spaces to be removed, or any kind of bracket around the sum of all 3 patterns to be interpreted as a group, where the first OR the second OR the third pattern is supposed to be catched.

And with all this I really have the impression, that you can't have both in GNU bash, version 4.3, multi pattern AND aside of that conditional a second conditional like "$name". Could that be? Or have I made a mistake in trying to acchieve that?

2
  • 4
    You’re grouping wrong. -py | --python | --python3,* means -py or --python or --python3,*. --python,surfer doesn’t match any of those patterns. Commented Apr 4, 2024 at 22:28
  • "or any kind of bracket around the sum of all 3 patterns to be interpreted as a group" -- Hehe, case statements don't have anything as fancy. It feels a bit surprising even alternation is there. Commented Apr 5, 2024 at 7:18

1 Answer 1

7

The object of your case clause needs to match properly:

case "$1","$name" in -py | --python | --python3,*) if [[ "$name" =~ \..+$ ]]; then 

should be

case "$1","$name" in -py,* | --python,* | --python3,*) if [[ "$name" =~ \..+$ ]]; then 

But this could probably be more clearly expressed with less repetition as:

if [[ "$name" =~ \..+$ ]]; then case "$1" in -py | --python | --python3) do_stuff ;; esac fi 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.