Tuesday, 8 October 2013

In some of the latest articles on 'Basic Linux Shell Scripting Language', we have learned what loops are, how they work, what their types are, syntax of each of them and a few examples. In this article, we will learn about Case Statement which is not a loop. Unlike For, While or Until loops, Case statement doesn't run a block of commands repeatedly, but it checks a condition and controls the flow of a program accordingly. Or it can be assumed as a simplified version of multilevel 'if-then-else' statement.

The 'Case' Statement

The Case statement executes the block of commands depending on a pattern matching decision. The expresion is matched against each pattern "PatternX" and when a match is detected, the associated Block of Commands BlockOfCommandsX is executed. Every BlockOfCommandsX is terminated by ;; so that shell will execute all the commands till ;; after a match. An esac at the end denotes end of the case statement.