Example of using the case statement

To specify an action when a word matches the pattern:

   cat diary
   #!/bin/sh
   today=`date +%m/%d`	(presents the date in the format 01/31)
   case	$today  in
   07/18)	echo	"Aonoch Mhor"
   ;;
   07/21)	echo	"Ben Wyvis"
   ;;
   08/02)	echo	"Buicheille Etive Mhor"
   ;;
   08/03)	echo	"Slioch"
   ;;
   *)		echo	"Wet..low level today"
   esac
   date +%m/%d
   07/18
   diary
   Aonoch Mhor 

The value for the word $today is generated by the date command. This is then compared with various patterns so that the appropriate commands are executed.

Note the use of the pattern *, this can be used to specify default patterns as the * character is the shell wildcard character.


[Home] [Search] [Index] This site maintained by staff@stacken.kth.se