1. Single-branch if condition statement
1 if [conditional judgment type]; Then 2 program 3fi4 or 5if [conditional judgment]6 then 7 program 8fi
Precautions:
The 1.if statement uses the fi end, and the general language uses curly braces to end the difference
2.[conditional judgment] is to use the test command to judge, so between the brackets and conditional judgment must have a space before and after the
3.then followed by the conditions after the execution of the program, can be placed after [], with ";" Semicolon split. You can also write with a newline, and you do not need the ";" The
Example: Judging partition utilization
Script Description: My root partition is/DEV/SDB5 I'm going to extract the hundred part of the fifth column in the df-h.
Assigned to $rate in the if to determine whether the comparison result is greater than 80 is greater than the establishment is not more than the establishment of the output warning is not established then do not perform any
Of course I can also replace the warning with other information such as email or trigger another warning script and generate a log of warning messages and so on!
#!/bin/bash# Statistics Root partition usage #author:pat (Email:239@qq. com) rate=$(DF-H |grep "/dev/sda5"|awk '{printf $ "\ n"}'|Cut-D"%"-F1) #把根分区使用率作为变量值赋予变量rateif[$rate-ge the ] Then Echo "warning!/dev/sda5 is full!!"fi
2. Dual-branch IF condition statement
1 if [Conditional judging type] 2 Then 3 When the condition is established, the program executed 4 Else 5 When the condition is not established, the program executed 6fi
Example 1: Backing up the MySQL database
1#!/bin/Bash2 #备份mysql数据库3Ntpdate asia.pool.ntp.org $>/dev/NULL4 #同步系统时间5 Date=$(Date+%y%m%d)6 #把当前系统时间的年月日格式赋予变量date7size=$ (du-SH/var/lib/MySQL)8 #统计mysql数据库文件的大小 and assign the size to the variable9 if[-d/tmp/Dbbak]Ten Then One Echo "Date: $date!">/tmp/dbbak/Dbinfo.txt A Echo "Data Size: $size">>/tmp/dbbak/Dbinfo.txt -cd/tmp/Dbbak - Tar-ZCF mysql-lib-$Date.Tar. Gz/var/lib/mysql Dbinfo.txt &>/dev/NULL the RM-rf/tmp/dbbak/Dbinfo.txt - Else - mkdir/tmp/Dbbak - Echo "Date: $date!">/tmp/dbbak/Dbinfo.txt + Echo "Data Size: $size">>/tmp/dbbak/Dbinfo.txt -cd/tmp/Dbbak + Tar-ZCF mysql-lib-$Date.Tar. Gz/var/lib/mysql Dbinfo.txt &>/dev/NULL A RM-rf/tmp/dbbak/Dbinfo.txt at fi
Example two: Determine if Apache starts:
Need to install Nmap command under CentOS execution: yum-y installl nmap
1#!/bin/Bash2 3port=$ (nmap-st localhost |grepTCP |grepHTTP |awk '{print $}')4 Echo "$port"5 # #使用nmap命令扫描服务器, and intercept the Apache service status, assign to the variable port6 if["$port"=="Open" ]7 Then8 Echo "$ (date) httpd is ok!">>/tmp/autostart-Acc.log9 ElseTen if[-F"/etc/init.d/httpd"] ; Then One/ETC/INIT.D/HTTPD Start &>/dev/NULL A Echo "$ (date) restart httpd !!">>/tmp/autostart-Err.log - Else - Echo "error is not httpd server your is Chack Apache?" the fi - fi
http.sh
3. Multi-branch if condition statement
1 if Then 2 when the current condition judgment 1 is established, the executed procedure 3elifthen4 The current condition judgment 2 is established, the procedure executed 5 ....... Can write multiple 6Else7 When all conditions are not true, the program executed 8fi
Example: 1
#!/bin/Bashread-P"Please input-a filename:" file# receive keyboard input and assign to variable fileif[-Z"$file"]# to determine if the variable file is empty Then Echo "Error Please input a filename"Exit1elif[!-E"$file"] ; Then# Determine if the value of file existsEcho "Your input is not a file!"Exit2elif[-F"$file"] ; Then Echo "is file"elif[-D"$file"] ; Then Echo "is directory"Else Echo "$file is a other file!"fi
Shell Learning Path: Process Control (IF)