4

How to use bash's "case" statement for "very specific" string patterns (multiple words, including spaces) ?

The problem is: I receive a multi-word String from a function which is pretty specific, including a version number. The version number now changes from time to time. Instead of adding more and more specific string patterns to my case statement, I would like to use a joker (or whatever the word for this is, like "Ubuntu 16.04.? LTS" or "Ubuntu 16.04.* LTS" . However, I didn't find any solution to this yet.

See this shell script I used so far:

    .
    .
    .
    case "${OS}" in
    "SUSE Linux Enterprise Server 11 SP4")
        echo "SLES11 detected."
        ;;
    "Ubuntu 16.04.3 LTS" | "Ubuntu 16.04.4 LTS"     )
        echo "UBUNTU16 detected."
        ;;
    "CentOS Linux 7 (Core)")
        echo "CENTOS7 detected."
        ;;
    *)
        echo "Unknown OS detected. Quitting for safety reasons."
        exit -1
        ;;
    .
    .
    .
Axel Werner
  • 978
  • 1
  • 9
  • 19
  • PS: i read the "pattern" part of the manual and tested some variations of "jokers", but it always failed, since i just forgot to simply drop the doublequotes and to escape the whitespaces. duh! – Axel Werner Apr 03 '18 at 14:17
  • 1
    I think the term you might be thinking of is "wildcard" (for joker) – Jeff Schaller Jul 01 '18 at 13:36

1 Answers1

4

You can use patterns in case statements, however you can't quote them so you have to escape whitespace.

case ${OS} in
"SUSE Linux Enterprise Server 11 SP4")
    echo "SLES11 detected."
    ;;
Ubuntu\ 16.04.[3-4]\ LTS)
    echo "UBUNTU16 detected."
    ;;
"CentOS Linux 7 (Core)")
    echo "CENTOS7 detected."
    ;;
*)
    echo "Unknown OS detected. Quitting for safety reasons."
    exit -1
    ;;
jesse_b
  • 35,934
  • 12
  • 91
  • 140