Case statement in shell script

Case is used to select one value from multiple option.

It is alternate and easy way to multi level if statement.

Case statement end with 'esac'.


script:
#!/bin/bash

echo "Which game you want to play ? "
echo "Option 1. Cricket
       2. Football
       3. Basketball
       4. Tennis
       5. Hockey"

echo -n "Enter your selection: "
read option
case "$option" in
    1) echo "Welcome to Cheppauk cricket stadium"
       echo "Cricket match will begin at 09.00am"
           ;;
    2) echo "Welcome to Delhi football stadium"
           echo "Football match will begin at 10.00am"
       ;;
    3) echo "Welcome to bangalore football court"
       echo "Basketball match will begin at 10.30am"
       ;;
    4) echo "Welcome to kolkata tennis court"
       echo "Tennis match will begin at 11.00am"
        ;;
    5) echo "Welcome to mumbai hockey stadium"
       echo "Hockey match will begin at 11.30am"
       ;;
    *) echo "Your selection is wrong !!!"
       ;;
esac

output:
i)
Which game you want to watch ?
Option 1. Cricket
       2. Football
       3. Basketball
       4. Tennis
       5. Hockey
Enter your selection: 2
Welcome to Delhi football stadium
Football match will begin at 10.00am


ii)
Which game you want to watch ?
Option 1. Cricket
       2. Football
       3. Basketball
       4. Tennis
       5. Hockey
Enter your selection: 3
Welcome to bangalore football court
Basketball match will begin at 10.30am

iii)
Which game you want to watch ?
Option 1. Cricket
       2. Football
       3. Basketball
       4. Tennis
       5. Hockey
Enter your selection: 5
Welcome to mumbai hockey stadium
Hockey match will begin at 11.30am

No comments:

Post a Comment