Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Tuesday, 8 October 2013

Basic Linux Shell Scripting Language : 'Case' Statement


    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.

Syntax:

 case $varName in
                pattern1)       
                    ---------------
                    BlockOfCommands1
                    ---------------  
                    ;;
                pattern2)
                    ---------------
                    BlockOfCommands2
                    ---------------
                    ;;  
                .....
                .....          
                patternN)       
                    ---------------
                    BlockOfCommandsN
                    ---------------                    
                    ;;
                *)              
          esac
OR

case $varName in
                pattern1|pattern2|pattern3)       
                    ---------------
                    BlockOfCommands1
                    ---------------
                    ;;
                pattern4|pattern5|pattern6)
                    ---------------
                    BlockOfCommands2
                    ---------------
                    ;;            
                pattern7|pattern8|patternN)       
                    ---------------
                    BlockOfCommandsN
                    ---------------                    
                    ;;
                *)              
esac

If you are familiar with C Language, you may find above syntax to be very much similar to that of 'Switch-case' statement, where the 'Default' statement is replaced by '*)' here.

Example 1:


#!/bin/bash

echo "Which is your Favorite Operating System..?"
read os

case $os in

"Linux") echo "Woww!! I am also a Linux Fan!!"
;;

"Mac") echo "You must be very Rich!"
;;

"Windows") echo "You Should Try Linux Once.. You would love it!"
;;

*) echo "I've never used that one!"

esac

Result:


Which is your Favorite Operating System..?
Mac
You Must be very Rich!


Example 2:


#!/bin/bash

echo "Which is your Favorite Operating System..?"
read os

case $os in

"Linux" | "Ubuntu" | "Linux Mint" | "CentOS") echo "Woww!! I am also a Linux Fan!!"
;;

"Mac") echo "You must be very Rich!"
;;

"Windows") echo "You Should Try Linux Once.. You would love it!"
;;

*) echo "I've never used that one!"

esac

Result:


Which is your Favorite Operating System..?
Linux Mint
Woww!! I am also a Linux Fan!!


That's all!

Also Read:

0 comments:

Post a Comment