case..esac..in Linux Shell Script

By: Dorris Emailed: 1757 times Printed: 2444 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

The case statement is good alternative to Multilevel if-then-else-fi statement. It enable you to match several values against one variable. Its easier to read and write.
Syntax:
           case  $variable-name  in
                pattern1)   command
                                ...
                                ..
                                command;;
                pattern2)   command
                                ...
                                ..
                                command;;
                patternN)   command
                                ...
                                ..
                                command;;
                *)             command
                                ...
                                ..
                                command;;
           esac

The $variable-name is compared against the patterns until a match is found. The shell then executes all the statements up to the two semicolons that are next to each other. The default is *) and its executed if no match is found. For e.g. write script as follows:

$ cat > car
#
# if no vehicle name is given
# i.e. -z $1 is defined and it is NULL
#
# if no command line arg

if [ -z $1 ]
then
  rental="*** Unknown vehicle ***"
elif [ -n $1 ]
then
# otherwise make first arg as rental
  rental=$1
fi

case $rental in
   "car") echo "For $rental Rs.20 per k/m";;
   "van") echo "For $rental Rs.10 per k/m";;
   "jeep") echo "For $rental Rs.5 per k/m";;
   "bicycle") echo "For $rental 20 paisa per k/m";;
   *) echo "Sorry, I can not gat a $rental for you";;
esac

Save it by pressing CTRL+D and run it as follows:
$ chmod +x car
$ car van
$ car car
$ car Maruti-800

First script will check, that if $1(first command line argument) is given or not, if NOT given set value of rental variable to "*** Unknown vehicle ***",if command line arg is supplied/given set value of rental variable to given value (command line arg). The $rental is compared against the patterns until a match is found.
For first test run its match with van and it will show output "For van Rs.10 per k/m."
For second test run it print, "For car Rs.20 per k/m".
And for last run, there is no match for Maruti-800, hence default i.e. *) is executed and it prints, "Sorry, I can not gat a Maruti-800 for you".
Note that esac is always required to indicate end of case statement.


Linux Home | All Linux Tutorials | Latest Linux Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Dorris
sum of all digits in a given number using Shell Script
sh -x and sh -v in Linux
case..esac..in Linux Shell Script
while loop in Linux Shell Script
Nested for loop in Linux Shell Script
for loop in Linux Shell Script
Multilevel if-then-else in Linux Shell Script
Nested if-else-fi in Linux Shell Script
if...else...fi in Linux Shell Script
test command or [ expr ] in Linux Shell Script
if condition in Linux Shell Script
Download Linux. Which linux and where?
Formula in Hibernate
Primary keys assigned by triggers in Hibernate
Assigned identifiers in Hibernate

More Tutorials in Linux
smskannel SMS gateway run in background
Running jar files in background in ssh window
Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697
Can't locate ExtUtils/MakeMaker.pm in @INC ...
bash: make: command not found
apxs: command not found
bash: svn: command not found
make: Nothing to be done for `all'.
How to burn your CD / DVD ISO image using Media Creator (Adaptec/Roxio) on Windows
How to burn your CD / DVD ISO image using Nero Burning ROM (Ahead Software) on Windows
How to burn your CD / DVD ISO image using Nero Express (Ahead Software) on Windows
How to burn your CD / DVD ISO image using NISO Recorder V2 Power Toy on Windows
How to burn your CD / DVD ISO image using k3b on CentOS
Compiling and Installing software from source in Linux
Installing RPM's using the Redhat Package Manager in Linux

More Latest News
Most Viewed Articles (in Linux )
Nested if-else-fi in Linux Shell Script
Sort numbers using arrays in Shell Script
Digital Clock in Shell Script
sum of all digits in a given number using Shell Script
bash: svn: command not found
Command line arguments to calculate average in Shell Script
Finding factorial in Shell Script
sh -x and sh -v in Linux
Check for leap year in Shell Script
for loop in Linux Shell Script
Decimal to hexadecimal converter in Shell Script
smskannel SMS gateway run in background
case..esac..in Linux Shell Script
Print contents of a file using Shell Script
while loop in Linux Shell Script
Most Emailed Articles (in Linux)
test command or [ expr ] in Linux Shell Script
Multilevel if-then-else in Linux Shell Script
Installing using Debian apt-get in Linux
smskannel SMS gateway run in background
Mounting cdrom in cent os 5 linux
How to use Linux?
What is a Shell Script in linux?
How to write your first Shell Script in linux?
Variables in Linux Shell Script
How to define User defined variables in Linux Shell Script
Rules for Naming variable names in Linux Shell Script
How to print or access value of user defined variables in Linux Shell Script
echo Command in Linux Shell Script
Arithmetic in Linux Shell Script
Quotes in Linux Shell Script