1. Flow control statement: case
( 1) Case statement format
The flow control statement is used to control the selection, loop, turn and return of the program flow. case is one of the components;
The Case statement compares values according to different variables, and then executes different command operations for different values .
Case statement format:
case variable or expression in variable or expression 1) command sequence 1 ;; variable or expression 2) command sequence 2 ;; …… *) default command sequence esac
( 2) Execution process
First use the value of "variable or expression" to compare with value 1. If the value is the same, execute the command sequence after value 1 until it meets the double semicolon ";;" and then jump to esac, which means the branch ends;
If it does not match the value 1, then continue to compare with the value 2, if the value is the same, execute the command sequence after the value 2 until it meets the double semicolon ";;" and jump to esac, which means the end branch.
By analogy, if no matching value is found , the command sequence after the default pattern "*)" is executed until the branch is ended after esac is met .
Precautions:
A. "Variable or expression" must be followed by the word in, and the value of each "variable or expression" must end with a closing parenthesis. The value can be a variable or a constant. After the match finds that the value matches a certain pattern, all commands in the meantime are executed until ;; ends.
B. The value in the match can be multiple values, separated by "|".
( 3) Case statement example
[root@test ~]# cat first-case.sh #!/bin/bash cat<<eof 1. Withdraw money 2. Deposit 3. Query 4. Withdraw card eof read -p "Please enter your option:" num case $num in 1) echo "withdraw money" ;; 2) echo "deposit money" ;; 3) echo "query" ;; 4) echo "withdraw card" ;; *) echo "please enter a valid number to choose, Valid range 1-4" esac [root@test ~]# [root@test ~]# sh first-case.sh 1. Withdraw money 2. Deposit money 3. Query 4.Take the card Please enter your choice: 6 Please enter a valid number to choose, the valid range is 1-4 [root@test ~]# sh first-case.sh 1. Withdraw money 2.Save money 3. Inquiry 4. Withdraw the card Please enter your option: 1 Withdraw money [root@test ~]#
B. Myslq process view script
[root@test ~]# vi case-mysql.sh #!/bin/bash read -p "Please enter start|stop|restart|status|option:" i case $i in start) /etc/init.d/ mysqld $i ps -ef|grep mysqld echo "mysql start" ;; stop) /etc/init.d/mysqld $i ps -ef|grep mysqld echo "mysql stop" ;; restart) /etc/init.d/ mysqld $i ps -ef|grep mysqld echo "mysql restart" ;; status) /etc/init.d/mysqld $i ;; *) echo "Please enter the correct option" esac root@test ~]# sh case-mysql. sh 请输入start|stop|restart|status|选项:start root 2018 1 1 23:04 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql mysql 2129 2018 3 23:04 pts/0 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock root 2148 1985 0 23:04 pts/0 00:00:00 grep mysqld mysql start [root@test ~]#
Personal public number: