Linux Shell Scripting Tutorial Series (13) Shell branch statement case ... esac tutorial

This article is the (thirteenth) part of the Linux Shell Scripting Series . For more Linux Shell tutorials, please see: Linux Shell Scripting Series Tutorials

After the last article , branch statements are very practical. Basically, high-level languages ​​support branch statements (python does not). Most of them use the switch … case format, but there is no switch … case in Shell, but don’t worry, Shell supports branch statements. Yes, but using the case ... esac format. Both are essentially the same.

 

1. Shell branch statement case esac syntax

case value in
Mode 1)
    command1
    command2
    command3
    ;;
Mode 2)
    command1
    command2
    command3
    ;;
*)
    command1
    command2
    command3
    ;;
esac

 Description: The case is followed by the value, the value is followed by the keyword in, followed by various matching patterns, each pattern must end with a closing parenthesis.

Values ​​can be variables or constants.

 

The pattern supports regular expressions and can use the following characters:

* any string
? any character
[abc] one of the three characters a, b, or c
[an] any character from a to n
| Multiple selection

 

After the match finds that the value matches a certain pattern, all commands during the period start to be executed until ;;.

;; Similar to break in other languages, it means skip to the end of the entire case statement without executing the following statement.

*) is similar to default, if there is no matching pattern above, the content in *) is executed.

 

2. Shell branch statement case...esac usage example

Learn the case...esac command with an example:

#!/bin/sh
#auther:linuxdaxue.com
#date:2016-05-30
case $1 in
start | begin)
    echo "I am started!"  
    ;;
stop | end)
    echo "I am stopped!"  
    ;;
*)
    echo "Other command!"  
    ;;
esac

 

Description: This script exercises pattern matching. Pattern matching supports the '|' character. If a condition is met, the command will be executed.

output:

$./test.sh start
I am started!
$./test.sh stop
I am stopped!
$./test.sh begin
I am started!
$/test.sh hello
Other command!

 

Next, I will show you an example of generating a menu through the case...esac statement:

#!/bin/bash  
#Author:linuxdaxue.com
#Date:2016-05-30
#Desc: Shell case menu exercise

clear # Clear the screen.  

echo "          Contact List"  
echo "          ------- ----"  
echo "Choose one of the following persons:"  
echo  
echo "[E]vans, Roland"  
echo "[J]ones, Mildred"  
echo "[S]mith, Julie"  
echo "[Z]ane, Morris"  
echo  

read person  

case "$person" in  
# Note that variables are referenced by "".  

"E" | "e" )  
# Accept uppercase or lowercase input.  
echo  
echo "Roland Evans"  
echo "4321 Floppy Dr."  
echo "Hardscrabble, CO 80753"  
echo "(303) 734-9874"  
echo "(303) 734-9892 fax"  
echo "[email protected]"  
echo "Business partner & old friend"  
;;  
# Note that every option must end with a double semicolon ;;.  

"J" | "j" )  
echo  
echo "Mildred Jones"  
echo "249 E. 7th St., Apt. 19"  
echo "New York, NY 10009"  
echo "(212) 533-2814"  
echo "(212) 533-9972 fax"  
echo "[email protected]"  
echo "Ex-girlfriend"  
echo "Birthday: Feb. 11"  
;;  

# The following information about Smith and Zane is omitted here.  

* )  
# Default options.  
# Empty input (enter RETURN), also works here.  
echo  
echo "Not yet in database."  
;;  

esac  

echo  

# practise:  
#  -----  
# Modify this script to accept multiple inputs,  
#+ and can display multiple addresses.  

exit 0

 

Description: This example mainly demonstrates how to use the case...esac statement to generate the menu, so that everyone can learn the usage of the case...esac statement more vividly and flexibly.

 

output:

Contact List
          ------- ----
Choose one of the following persons:

[E]vans, Roland
[J]ones, Mildred
[S]mith, Julie
[Z] ane, Morris

E

Roland Evans
4321 Floppy Dr.
Hardscrabble, CO 80753
(303) 734-9874
(303) 734-9892 fax
[email protected]
Business partner & old friend

 

The above is the output of the input E parameter, input S or Z, the output will be as follows:

Contact List
          ------- ----
Choose one of the following persons:

[E]vans, Roland
[J]ones, Mildred
[S]mith, Julie
[Z] ane, Morris

WITH

Not yet in database.

 

Well, let's introduce the usage of branch statement case...esac in Shell first. case...esac is a very powerful command, and you can use it to do a lot of things. This article is just for introductory purposes. More needs everyone to try and practice a lot in actual use.

For more Linux Shell tutorials, please see: Linux Shell Scripting Series Tutorials

 

Original: Linux Shell series of tutorials (thirteen) Shell branch statement case ... esac tutorial

Previous: Linux Shell Scripting Tutorial Series (12) Shell until loop

Next: Linux Shell Scripting Tutorial Series (14) Shell Select Tutorial

This article is transferred from: Linux Shell Scripting Tutorial Series (13) Shell branch statement case ... esac tutorial

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326580897&siteId=291194637