Decision Making in UNIX Shell Scripts - case Statement

The case statement allows decision making when the decisive value leads to multiple paths. The logic can be implemented using if-elifs also, but case construct makes it less complex.

Syntax:

case value in
pattern1) commands ;;
.
.
.
esac

Example:

The example script reads an input from std input and stores it in the variable MONTHNUM. The case construct processes the month number and outputs corresponding month in words. "The input should be a 2 digit number and less than 13" is printed as output.

#!/bin/sh

read MONTHNUM?"Enter month in mm format"

case $MONTHNUM in
   01) print January ;;
   02) print February ;;
   03) print March ;;
   04) print April ;;
   05) print May ;;
   06) print June ;;
   07) print July ;;
   08) print August ;;
   09) print September ;;
   10) print October ;;
   11) print November ;;
   12) print December ;;
   *)  print "The input should be a 2 digit number and less than 13" ;;
esac

exit 0

Comments

Popular posts from this blog

OS/DB Migration - CMD. STR, TOC, EXT, R3load, DDLDBS.TPL and more

Fixing Inconsistent Table - Table activation fails due to inconsistency between DD and DB

301 Redirect Using SAP Web Dispatcher