lördag 7 mars 2015

Linux bash The case statement is good alternative to multilevel if-then-else-fi statement. It enable you to match several values against one variable. It is easier to read and write.

The case statement is good alternative to multilevel if-then-else-fi statement. It enable you to match several values against one variable. It is easier to read and write.

Syntax

The syntax is as follows:

          case  $variable-name  in
                pattern1)       
     		    command1
                    ...
                    ....
                    commandN
                    ;;
                pattern2)
     		    command1
                    ...
                    ....
                    commandN
                    ;;            
                patternN)       
     		    command1
                    ...
                    ....
                    commandN
                    ;;
                *)              
          esac 
          case  $variable-name  in
                pattern1|pattern2|pattern3)       
     		    command1
                    ...
                    ....
                    commandN
                    ;;
                pattern4|pattern5|pattern6)
     		    command1
                    ...
                    ....
                    commandN
                    ;;            
                pattern7|pattern8|patternN)       
     		    command1
                    ...
                    ....
                    commandN
                    ;;
                *)              
          esac 
  • The case statement allows you to easily check pattern (conditions) and then process a command-line if that condition evaluates to true.
  • In other words the $variable-name is compared against the patterns until a match is found.
  • *) acts as default and it is executed if no match is found.
  • The pattern can include wildcards.
  • You must include ;; at the end of each commandN. The shell executes all the statements up to the two semicolons that are next to each other.
  • The esac is always required to indicate end of case statement.

Example

Create a shell script called rental.sh:

#!/bin/bash
 
# if no command line arg given
# set rental to Unknown
    

  rental="*** Unknown vehicle ***"
    

# otherwise make first arg as a rental
  rental=

 
# use case statement to make decision for rental
 $rental 
   "car"  "For $rental rental is Rs.20 per k/m."
   "van"  "For $rental rental is Rs.10 per k/m."
   "jeep"  "For $rental rental is Rs.5 per k/m."
   "bicycle"  "For $rental rental 20 paisa per k/m."
   "enfield"  "For $rental rental Rs.3  per k/m."
   "thunderbird"  "For $rental rental Rs.5 per k/m."
     "Sorry, I can not get a $rental rental  for you!"

Save and close the file. Run it as follows:

chmod +x rental.sh
.rental.sh 
.rental.sh jeep
.rental.sh enfield
.rental.sh bike

Sample outputs:

Sorry, I can not get a *** Unknown vehicle *** rental  for you!
For jeep rental is Rs.5 per k/m.
For enfield rental Rs.3  per k/m.
Sorry, I can not get a bike rental  for you!

The case statement first checks $rental against each option for a match. If it matches "car", the echo command will display rental for car. If it matches "van", the echo command will display rental for van and so on. If it matches nothing i.e. * (default option), an appropriate warning message is printed.

Using Multiple Patterns

#!/bin/bash
=$ +
  
	Mon	
		 "Full backup"
	TueWedThuFri
		 "Partial backup"
	SatSun	
		 "No backup"
	 

The following shell script demonstrate the concept of command line parameters processing using the case statement (casecmdargs.sh):

#!/bin/bash
=   # option
=  # filename
 
# test -e and -E command line args matching
  
  -E 
  	 "Editing $2 file..." 
        # make sure filename is passed else an error displayed   
  	  $FILE     "File name missing";  ;    $FILE	
  	
  -C 
  	 "Displaying $2 file..." 
  	  $FILE     "File name missing";  ;    $FILE	
  	
  -D 
  	 "Today is $(date)" 
  	
    
     "Bad argument!" 
     "Usage: $0 -ecd filename"
     "	-e file : Edit file."
     "	-c file : Display file."
     "	-d      : Display current date and time."	
    

Run it as follows:

chmod +x casecmdargs.sh
.casecmdargs.sh
.casecmdargs.sh  tmp
.casecmdargs.sh  tmp
.casecmdargs.sh  
.casecmdargs.sh 

Creating a backup script

Create a backup script called allinonebackup.sh:

#!/bin/bash
# A shell script to backup mysql, webserver and files to tape
=
  
        sql
                 "Running mysql backup using mysqldump tool..."
                
        
                 "Running backup using rsync tool..."
                
        
                 "Running tape backup using tar tool..."
                
        
        	     "Backup shell script utility"
                 "Usage: $0 {sql|sync|tar}"
                 "	sql  : Run mySQL backup utility."
                 "	sync : Run web server backup utility."	
                 "	tar  : Run tape backup utility."	

Save and close the file. Run it as follows:

chmod +x allinonebackup.sh
# run sql backup
.allinonebackup.sh sql
# Dump file system using tape device
.allinonebackup.sh 
# however, the following will fail as patterns are case sensitive
# you must use command line argument tar and not TAR, Tar, TaR etc. 
.allinonebackup.sh TAR

Inga kommentarer: