SH script case Conditional statement usage in Linux

Source: Internet
Author: User
Tags case statement


Case statements are useful for applications where multiple branches are required.
The case Branch statement is formatted as follows:
Case variable name in
Mode 1)
Command sequence 1
;;
Mode 2)
Command Sequence 2
;;
*)
Sequence of commands executed by default
Esac
The structure characteristics of case statement are as follows:
The end of the case line must be the word "in" and each pattern must be closed with a closing parenthesis ")".
Double semicolon ";;" Represents the end of a command sequence.
The matching pattern uses square brackets to denote a contiguous range, such as [0-9]; Use the vertical bar symbol "|" Represents or.
The Last "*" indicates the default mode, when the variable is not matched using the preceding modes, the "*" is executed
Sequence of commands.

Case Statement instance: the user enters a character from the keyboard and determines whether the character is a letter, number, or other character.
and output the corresponding hint information.

#!/bin/bash
Read-p "Press some key, then press RETURN:" Key
Case $KEY in
[a-z]| [A-z]
echo "It ' s a letter."
;;
[0-9])
echo "It ' s a digit."
;;
*)
echo "It ' s function keys, SPACEBAR or other ksys."
Esac

######################################################
Case word in [pattern [| pattern] ...) list; ] ... Esac
The standard usage of CASE/ESAC is roughly as follows:
Case $arg in
Pattern | Sample) # arg in-pattern or sample
;;
PATTERN1) # arg in PATTERN1
;;
*) #default
;;
Esac
ARG is the parameter that you introduce if the ARG content conforms to the pattern project,
Then the code following pattern is executed, and the code is two semicolon ";;" Do the end.

It can be noted that "case" and "Esac" is symmetrical, if not remembered, the word is reversed.

--------------------------------------------------------------------------------

Example one: Paranoia
#!/bin/sh
Case is in
Start | Begin
echo "Start Something"
;;
Stop | End
echo "Stop Something"
;;
*)
echo "Ignorant"
;;
Esac

Perform
[Foxman@foxman bash]# chmod 755 Paranoia
[Foxman@foxman bash]#./paranoia
Ignorant
[Foxman@foxman bash]#./paranoia start
Start something
[Foxman@foxman bash]#./paranoia begin
Start something
[Foxman@foxman bash]#./paranoia stop
Stop something
[Foxman@foxman bash]#./paranoia End
Stop something

--------------------------------------------------------------------------------

Case two: Inetpanel
Many of the daemon will be accompanied by a management Shell Script,
Like bind, attach the Ndc,apache and attach the Apachectl. These management programs are written in shell script,
A shell script that manages inetd is shown below.
#!/bin/sh

Case is in
Start | Begin | Commence)
/usr/sbin/inetd
;;
Stop | End | Destroy
Killall inetd
;;
Restart | Again
Killall-hup inetd
;;
*)
echo "Usage:inetpanel" [Start | Begin | Commence | Stop | End | Destory | Restart | Again] "
;;
Esac

--------------------------------------------------------------------------------

Example Three: Judgment system
Sometimes, the script you write may span several different platforms,
such as Linux, FreeBSD, Solaris, and so on, and between platforms,
There are more or less differences, and sometimes it needs to be judged on which platform is being executed.
At this point, we can use uname to find out the system information.
#!/bin/sh

System= ' Uname-s '

Case $SYSTEM in
Linux)
echo "My system is Linux"
echo "Do Linux stuff ..."
;;
FreeBSD)
echo "My system is FreeBSD"
echo "Do FreeBSD stuff ..."
;;
*)
echo "Unknown system: $SYSTEM"
echo "I don ' t what to do ..."
;;
Esac
<!--[If!supportlinebreaknewline]-->
<!--[endif]-->

Summarize:

Using "" is not mandatory for variables, because Word segmentation does not occur.
Each test line ends with a closing parenthesis.
Each conditional judgment statement block ends with a semicolon;;.
The case block ends with a esac (reverse spelling of the case).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.