Shell Script Programming Learning Note-case statement

Source: Internet
Author: User
Tags case statement pear

1.case structural Conditional statement syntax

The case statement is actually the canonical multi-branch if statement

Case "string variable" in

Value 1) Instruction 1 ...

;;

Value 2) Instruction 2 ...

;;

*) Instruction 3 ...

Esac

Chinese programming Syntax:

Case "Find a girlfriend condition" in

Have a room) to marry you ...

;;

Your father is Li Gang) to marry you ...

;;

Hard to endure, you can think about friends first ...

;;

*) Good bye!!!

Esac

2. Simple Case Script

Inputs 1, 2, 3 respectively output corresponding values

[[email protected] jiaobenlianxi]# cat case01.sh #!/bin/bashusage() {echo "USAGE:$0 {1|2|3}" contentsexit 1}num() {case "$1" in1)echo "1";;2)echo "2";;3)echo "3";;*)usageesac}main() {    if [ $# -ne 1 ];then    usage       fi      num $1}main $*
3. Execute the script print a fruit menu as follows:

A.apple

B.pear

C.banana

D.cherry

When the user chooses the fruit, the print tells it what fruit is selected and adds color to the selected fruit. Requires a case statement implementation.

[[email protected] jiaobenlianxi]# cat menufruit.sh #!/bin/bashRED_COLOR=‘\E[1;31m‘GREEN_COLOR=‘\E[1;32m‘YELLOW_COLOR=‘\E[1;33m‘BLUE_COLOR=‘\E[1;34m‘PINK=‘\E[1;35m‘SHAN=‘\E[31;5m‘  提示闪烁功能结合 echo –e 使用RES=‘\E[0m‘menu(){cat <<EOF 1.[ apple ] 2.[ pear ] 3.[ bananan ] 4.[ cherry ] 5.exitEOF    read -p "Please input a fruit:" fruit}usage(){    echo -e ${SHAN}"please select the exitnum behind"${RES}     echo "==========================================="}fruit(){case "$fruit" in    1)echo -e "${RED_COLOR} apple $RES";;    2)echo -e "${GREEN_COLOR} pear $RES";;    3)echo -e "${YELLOW_COLOR} bananan $RES";;    4)echo -e "${BLUE_COLOR} cherry $RES";;    5)    exit;;    *)usageesac}main(){    while true    do        menu        fruit    done}Main
4. Job

The Nginx management commands are known to be:

Start:/usr/local/nginx/sbin/nginx

Stop:/usr/local/nginx/sbin/nginx–s stop

Reload:/usr/local/nginx/sbin/nginx–s Reload

Please use case script to simulate Nginx service startup shutdown:

/etc/init.d/nginx {Start|stop|restart|reload}

and implementation can be managed through chkconfig.

Actual operation:

Actually very simple, we can divide four modules the first module is to start the service module, the second module is to shut down the service module, the third module is to restart the service module, the fourth module is a smooth restart module. First use the function to implement these four modules, in the call these functions, the last is to set the boot, which requires the use of chkconfig command. First of all, we need to put the startup script we wrote under/etc/init.d/, and then put it under boot from boot.

We will first rename the startup script and then put it under/etc/init.d/and give execute permission.

[[email protected] jiaobenlianxi]# cp nginx.sh nginx[[email protected] jiaobenlianxi]# cp nginx /etc/init.d/[[email protected] jiaobenlianxi]# chmod +x /etc/init.d/nginx [[email protected] jiaobenlianxi]# ll /etc/init.d/nginx  -rwxr-xr-x. 1 root root 981 4月   2 04:16 /etc/init.d/nginx

Add power-on self-boot

Before adding boot-up, we need to know at what level of service the service is running.
Take network as an example to view the Network service boot-up RunLevel

[[email protected] jiaobenlianxi]# chkconfig --list networknetwork 0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭

Grade:

0: Shut down the machine;

1: Single user mode;

2: Multi-user mode, no NFS;

3: Standard multi-user mode;

4: Not available;

5:x11, graphical interface mode;

6: Reboot.

Above is the command line to view the network start-up runlevel, below we see in the network startup script, the network boot-up level.

[[email protected] jiaobenlianxi]# head -5 /etc/init.d/network #! /bin/bash## network   Bring up/down networking## chkconfig: 2345 10 90# description: Activates/Deactivates all network interfaces configured to \

We look at the Chkconfig line above, this is very important it defines the boot order by default is 2345, 10 is the service boot sequence, 90 represents the service shutdown boot sequence we will chkconfig and description this line copy to/etc/ Init.d/in the Nginx startup script. We first go to/etc/rc.d/rc3.d/inside look for no use of boot boot sequence, we see S20 is no we will be the Nginx boot sequence set to 20. Close order the same is true, this does not demonstrate the shutdown sequence to start with capital K, we set the nginx shutdown sequence to 16.

[[email protected] logs]# ll /etc/rc.d/rc3.d/ |grep S19lrwxrwxrwx. 1 root root 17 12月 30 04:10 S19rpcgssd -> ../init.d/rpcgssd[[email protected] logs]# ll /etc/rc.d/rc3.d/ |grep S20

The script code is as follows:

[email protected] jiaobenlianxi]# cat nginx.sh #!/bin/bash# chkconfig:2345 16# Description:nginx is a HTTP serv  er#date:2018-04-07 #Author: Create by Linzhongniao#mail: [email protected] #Function: The scripts Function is Nginx startup script. #Version: 1.1 If [-f/etc/init.d/functions];then. /etc/init.d/functionsfipidfile=/usr/local/nginx/logs/nginx.pidshan= ' \e[31;5m ' RES= ' \E[0m ' Nginx=/usr/local/nginx    /sbin/nginxretval=0linzhongniao () {retval=$?    If [$RETVAL-eq 0];then Action "Nginx is $"/bin/true else Action "Nginx is $"/bin/true Fi}start () { If [-F $pidfile];then echo-e ${shan} "Nginx is running" ${res} else $nginx Linzhongniao Starte D fi return $RETVAL}stop () {if [!-F $pidfile];then echo-e ${shan} "Nginx is stopped" ${res} els E $nginx-S stop Linzhongniao stopped fi return $RETVAL}restart () {printf "restarting Nginx ... \ N "Stop sleep 2 start}rEload () {if [!-F $pidfile];then echo-e ${shan} "Can ' t open $pidfile, no such file or directory" ${res} else $nginx-S Reload Linzhongniao reload fi return $RETVAL}usage () {echo-e ${shan} "usage:$0 {start|  Stop|restart|reload} "${res}}main () {case" $ "in start) start;;  stop) stop;;  restart) restart;;  reload) reload;; *) usage exit $RETVAL esac}main $1exit $RETVAL

Finally we load it into the Chkconfig, complete the Nginx service boot from the boot

[[email protected] init.d]# chkconfig nginx on[[email protected] init.d]# chkconfig --list nginxnginx   0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭关闭开机自启动[[email protected] init.d]# chkconfig nginx off[[email protected] init.d]# chkconfig --list nginxnginx   0:关闭    1:关闭    2:关闭    3:关闭    4:关闭    5:关闭    6:关闭
5.case Statement Summary

(1) The case statement is equivalent to a multi-branch if statement. Case statement advantages are more standardized and readable.

(2) A case statement is suitable for a set of numbers or strings that are of a variable value and are fixed. (Start,stop,restart) or (a).

(3) System service startup script to use the case statement, refer to the/etc/init.d/rsyslog startup script.

(4) All case statements can be implemented with an if, but case statements are more prescriptive.

(5) The case statement is generally appropriate for the service's startup script.

(6) The value of the variable of case if the element of the known fixed start/restart/stop is more appropriate.

Statement Summary:

(1) Case is mainly written startup script, the scope is narrower.

(2) If the value is judged, compared and widely used.

6. Learning System Scripts

Multi-directional System scripting Learning

/etc/init.d/functions

function library functions Detailed: http://www.cnblogs.com/image-eye/archive/2011/10/26/2220405.html

/etc/rc.d/rc.sysinit

/etc/init.d/rpcbind

/etc/init.d/nfs

/etc/init.d/httpd

Shell Script Programming Learning Note-case statement

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.