- Single-branch If condition statement
If [conditional judgment]; Then
Program
Fi
Example: Judging partition usage
#!/bin/bash# Author:huangrui (email:[email protected]) rate=$(DF-H |grep "sda1"|awk '{print $}'|Cut-F1-D"%") if[$rate-GT the]; Then Echo "warning! disk is full!!!"fi
Code
Dual-Branch IF condition statement
If [conditional judgment]; Then
When a condition is executed, the statement executed
Else
Conditional execution is not valid, the executed statement
Fi
Example: Backing up a site directory file
#!/bin/bash# Author Huangrui (email:[email protected])Date=$(Date+%y%m%d) Size=$(du-SH/home/test.com)if[-d/home/test.com]; Then Echo "date is: $date">/tmp/dbback/Db.txtEcho "size is: $size">>/tmp/dbback/db.txt CD/tmp/DbbackTar-ZCF web_$Date.Tar. gz/home/test.com db.txt&>/dev/NULL RM-rf/tmp/dbback/Db.txtElse mkdir/tmp/DbbackEcho "date is: $date">/tmp/dbback/Db.txtEcho "size is: $size">>/tmp/dbback/db.txt CD/tmp/DbbackTar-ZCF web_$Date.Tar. gz/home/test.com db.txt&>/dev/NULL RM-rf/tmp/dbback/Db.txtfi
CodeTo determine if Apache is starting, use Nmap command
#!/bin/bash# Author Huangrui (email:[email protected]) port=$ (nmap-st172.16.193.128|grepTCP |grepHTTP |awk '{print $}') if["$port"=="Open"]; Then Echo "$ (date) httpd is ok!">>/tmp/autostart-Acc.logElse/ETC/RC.D/INIT.D/HTTPD Restart &>/dev/NULL Echo "$ (date) httpd is restart!!">>/tmp/autostart-Err.logfi
Code
Multi-branch IF condition statement
If [conditional judgment 1]; Then
When the conditional judgment 1 is established, the execution of the program 1
elif [Conditional judgment type 2]; Then
When the conditional Judgment 2 is established, the execution of the program 2
... Omit more conditions ...
Else
When all conditions are not set, the final execution of this program
Fi
Shell Process Control-IF statement