Bash Shell scripting – The case statement




The Case Statement
The case statement is used to execute statements based on specific values. Often used in place of an if statement, if there are a large number of conditions.

Syntax:

In the above syntax:

    • Value used can be an expression
    • each set of statements must be ended by a pair of semicolons;
    • a *) is used to accept any value not matched with list of values

Example 1 :

    • In the above example, the statement block following the *) case is executed if the $# variable does not match any of the previous cases (4,3,2, or 1).
    • A double semicolon indicates the end of the statement block for each case.
    • The esac keyword (“case” spelled backward) is used to indicate the end of the case statement.
    • After one of the statement blocks in the case statement is executed (such as echo (“Ready to process four files. “), the next statement executed is the line after the esac keyword.

Example 2 :

Output:


Example 3 :


Example 4 :

Output

[a-z] ) specifies a range which matches any lowercase letter from “a” to “z”
[A-Z] ) specifies a range which matches any lowercase letter from “A” to “Z”
[0-9] ) specifies a range which matches any value 0 to 9″ ;;
? )  Matches a string with exactly one character like a, !, and so on.
* ) Matches a string with one or more characters (a nonempty string).


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com