Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make switch execute 2 cases

how to make switch execute 2 cases?

I tried with the following code, but it execute only the first case

#!/bin/sh
action="titi"

case "$action" in
    toto|titi)
    echo "1_$action"
    ;;
    tata|titi)
    echo "2_$action"
    ;;
esac
like image 311
MOHAMED Avatar asked Dec 11 '22 13:12

MOHAMED


1 Answers

The case statement in bash executes the commands in the COMMAND-LIST for the first match only.

However, In bash version 4 or later introduced the ;& terminator. The ;;& operator is like ;;, except the case statement doesn't terminate after executing the associated list - Bash just continues testing the next pattern as though the previous pattern didn't match. Using these terminators, a case statement can be configured to test against all patterns, or to share code between blocks, for example.

Reference: Excerpt taken from http://wiki.bash-hackers.org/syntax/ccmd/case

So if you have bash v 4 or later this would give your desired result:

#!/bin/sh
action="titi"

case "$action" in
    toto|titi)
    echo "1_$action"
    ;;&
    tata|titi)
    echo "2_$action"
    ;;
esac
like image 180
jaypal singh Avatar answered Jan 01 '23 01:01

jaypal singh