skip to main content

SAP NetWeaver Newbie

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

No comments:

Post a Comment

Email Subscription

Get every new post into your inbox by subscribing us.

Want a reason to subscribe?
1. This sitemap might convince you to subscribe.
2. We do not misuse email IDs. We respect privacy.

© 2008 - 2017 sapnwnewbie. All rights reserved.