2014-01-24

shell script:case statement usage

Pranaam to all Bhai ji _/\_
 Today we will learn how to use 'case statement' in shell scripting.
case statement is used when we want to perform some action on basis of user input and provide option to user for performing different different task , task will be dependent on user input .
in case statement , we can define action for many inputs and for each input we define what has to done .
how????
lets start :)

Syntax for case statement:-

case  variable  in
        value1)
              statement/action
           ;;
       value2)
              statement/action
             ;;
        value3)
               statement/action
             ;;
......
......
....
       valueN)
            statement 
           ;;
        
             *)
              statement
            ;;
esac

here 
variable is the variable which will be matched with variable defined in case statement block and action will be performed according to matched variable.
value1 is the first value in case statement 
for example 
if variable value is 'a' and value1 is also 'a'  , in that case statement/command defined in value1 block will start executing 
if value2 is 'b' and value in variable is also 'b' , statement/action defined in value2 block will be executed 
and so far :)
every value block will be ended with ;;
value)
statement 1
statement 2
...
..
statement n
;;

*) is the default action block , mean if non of the value matched then what should be performed.
like if variable value doesnt match with non of the value defined in case statement , then print "please provide a correct value "
case statement block ends with "esac" keyword 
ok lets have an example :)
suppose user input value 1 , script will print "good", if user input value 2 , script will print "very good" and if value is neither 1 nor 2 , script will print  "please enter either value 1 or 2" 
script code is 

echo -e "            please enter a value    "
echo -e "     i will execute statement defined in case statement block \n    "
read var
case $var in
          1)
echo "good"
;;
          2)
echo "very good"
;;
           *)
echo "please enter either value 1 or 2"
;;

esac



ok now lets code something that can help you in system administration ;)
okkkkk lets use case statement and code a script that will start/stop/restart apache server :)
concept is simple 
script will print message , that user can input 1 for starting apache server, 2 fro stopping it and 3 for restarting     apache server 
commands for starting/stopping/restarting apache server are

Start:-
for centos/redhat/fedora
service httpd start   
for ubuntu 
service apache2 start

Stop:-
for centos/redhat/fedora
service httpd stop   
for ubuntu 
service apache2 stop

Restart:-

for centos/redhat/fedora
service httpd restart   
for ubuntu 
service apache2 restart

now we have to run legitimate command according to user input 
so code for script will be

echo -e  "\n    This script is for start/stop/restart apache server \n"
echo -e  "    Type 1 for starting, 2 for stopping and 3 for restarting apache server \n "

read action
case $action in 
    1)
    echo -e  "   i am starting apache server ... please wait .... \n"
    cmd='service httpd start'
      $cmd
     ;;
    
    2)
    echo -e  "   wait bhai ji , i am executing command to stop apache server  \n"
    cmd='service httpd stop'
      $cmd

      ;;

    3)
    echo -e  "   apache server is going to restart ... please wait .... \n"
    cmd='service httpd restart'
      $cmd

      ;;

    *)
    echo -e  "   Drinking too much alcohol is very bad :) .. \n"
      ;;
esac

save this script and execute 
you will get screen like this 


provide input according to action which you want to perform 
like i want to stop apache server, so i am going to type 2 and then script processed it like this 


ok, if i will specify input other then 1,2 or 3 , script will perform action defined in *) section and will print "drinking too much alcohol is really bad" , its because i defined to echo this line if input is not 1,2 or 3


so it was an introduction , how to use case statement in shell scripting . 
Thank you :)

Greetz to :- Guru ji Zero , code breaker ica, Aasim shaikh,Reborn, Raman kumar rana,INX_r0ot,Darkwolf indishell, Chinmay Pandya,L0rd Crus4d3r,Hackuin ,Silent poison India,Magnum sniper,Atul Dwivedi,ethicalnoob Indishell,Local root indishell,Irfninja indishell Hardeep bhai,Mannu,Viki and AR AR bhai ji <3

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

© 2009 Start With Linux | Mannu Linux
Designed by cyb3r.gladiat0r
Posts RSSComments RSS
Back to top