User10
User10

Reputation: 89

executing a pattern in a case statement that matches two expressions

#!/bin/bash
hw() {
        echo "hello world"
}

h0() {
        echo "hello w0r16"
}

h3w() {
        echo "h3ll0 world"
}

h30() {
        echo "h3110 w0rld"
}


read -p "enter 1 for hello and 2 for h3110: " hello
read -p "enter A for world and B for w0r16: " world1

option=$hello+$world1

case $option in 
    1+A|a) hw;;
    1+B|b) h0;;
    2+A|a) h3w;;
    2+B|b) h30;;
    5) echo "You have chosen to exit script" && exit 1;;
esac

how do i match both the inputs hello and world1 to the case statement. so it can find the match. example if i enter 1 for the first input and a for the second one. how do i match both inputs to find hw where the output would be hello world.

Upvotes: 0

Views: 24

Answers (1)

ilkkachu
ilkkachu

Reputation: 6517

You can use brackets to look for one of a set of characters:

$ foo=1; bar=B
$ option=$foo+$bar
$ case $option in 1+[bB]) echo 1b;; esac
1b

Alternation would apply to the whole pattern, i.e. 1+A|a looks for either 1+A or a but not 1+a.

Though in this case, I would generate the "hello"/"h3ll0" and "world"/"w0r16" separately. You'd still have four cases total, but could save on duplication when generating the strings.

Upvotes: 1

Related Questions