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:

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

case$varin

val1)

statements;;

val2)

statements;;

*)

statements;;

esac

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 :

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#! /bin/bash

case$# in

4)

echo"Ready to process four files. ";;

3)

echo"Ready to process three files.";;

2)

echo"Ready to process two files.";;

1)

echo"Ready to process one file.";;

*)

echo"Invalid number of filenames provided, ";;

esac

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 :

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

#! /bin/bash

vehicle=$1

case$vehiclein

"car")

echo"Rent of $vehicle is 100 dollar";;

"van")

echo"Rent of $vehicle is 80 dollar";;

"bicycle")

echo"Rent of $vehicle is 5 dollar";;

"truck")

echo"Rent of $vehicle is 150 dollar";;

*)

echo"Unknown vehicle";;

esac

Output:

1

2

3

4

5

6

7

8

$./bash.sh

Unknown vehicle

$./bash.sh car

Rent of car is100dollar

$./bash.sh van

Rent of van is80dollar

Example 3 :

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

#!/bin/bash

echo-n"Enter a number 1 < x < 10: "

read-rx

case$xin

1)echo"Value of x is 1.";;

2)echo"Value of x is 2.";;

3)echo"Value of x is 3.";;

4)echo"Value of x is 4.";;

5)echo"Value of x is 5.";;

6)echo"Value of x is 6.";;

7)echo"Value of x is 7.";;

8)echo"Value of x is 8.";;

9)echo"Value of x is 9.";;

0|10)echo"wrong number.";;

*)echo"Unrecognized value.";;

esac

Example 4 :

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

#! /bin/bash

echo-e"Enter some character : \c"

readvalue

case$valuein

[a-z])

echo"User entered $value a to z";;

[A-Z])

echo"User entered $value A to Z";;

[0-9])

echo"User entered $value 0 to 9";;

?)

echo"User entered $value special character";;

*)

echo"Unknown input";;

esac

Output

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

test@test$./hello.sh

Enter some character:f

User enteredfatoz

test@test$./hello.sh

Enter some character:K

User enteredKatoz

test@test$LANG=C

test@test$./hello.sh

Enter some character:K

User enteredKAtoZ

test@test$./hello.sh

Enter some character:9

User entered90to9

test@test$./hello.sh

Enter some character:5

User entered50to9

test@test$./hello.sh

Enter some character:&

User entered&special character

test@test$./hello.sh

Enter some character:sdsdsdsd

Unknown input

[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).