List

Syntax

case word in
   pattern1)
      Statement(s) to be executed if pattern1 matches
      ;;
   pattern2)
      Statement(s) to be executed if pattern2 matches
      ;;
   pattern3)
      Statement(s) to be executed if pattern3 matches
      ;;
   *)
     Default condition to be executed
     ;;
esac

Example

#!/bin/sh

FRUIT="kiwi"

case "$FRUIT" in
   "apple") echo "Apple pie is quite tasty." 
   ;;
   "banana") echo "I like banana nut bread." 
   ;;
   "kiwi") echo "New Zealand is famous for kiwi." 
   ;;
esac

Case Day Wise Decision Output

NOW=$(date +”%a”)
case $NOW in Mon) echo “Full backup”;;
Tue|Wed|Thu|Fri) echo “Partial backup”;;
Sat|Sun) echo “No backup”;;
*) ;;
esac

Case Transport Mode

mode = “bike”;
case $mode in “sportscar”) echo “For $mode, rent is Rs.20 per k/m.”;;
“lorry”) echo “For $mode, rent is Rs.50 per k/m.”;;
“sumo”) echo “For $mode, rent is Rs.30 per k/m.”;;
“bicycle”) echo “For $mode, rent is Rs. 5 per k/m.”;;
*) echo “Sorry, I can not get a $mode rent for you!”;;
esac

Leave a Reply

Your email address will not be published. Required fields are marked *