01. Judging by File type =
Common-D,-E,-F
Two types of formats:
TEST-E/root/install.log = Common
[-e/root/install.log] = both spaces must have
Example: [-d/root] && echo "Yes" | | echo "No": Determine if the file exists and whether it is a directory file, yes, not No.
02, according to the file permissions to judge
Comparison between 03, two files
Comparison between 04 and two numbers + =
05. Single Branch If condition statement
If [conditional judgment];then = = Condition judgment Both spaces cannot be omitted
Program
Fi
Or
If [conditional judgment]
Then
Program
Fi
Example: Judging partition usage
#!/bin/bash
#统计根分区使用率
# Author:shencao (E-mail:[email protected])
rect=$ (df-h | grep/dev/sda5 | awk ' {print $} ' | cut-d "%"-f1)
If [$rect-ge 10]
Then
echo "warning! /dev/sda5 is full!! "
Fi
06. Multi-Branch If statement
If [conditional judgment];then = = Condition judgment Both spaces cannot be omitted
Program
Fi
Or
If [conditional judgment]
Then
Procedure for establishing conditions
Else
Conditions do not form a program
Fi
Example: Backing up a MySQL database
#!/bin/bash
#备份 MySQL Database
# Author:shencao (E-mail:[email protected])
date=$ (Date +%y%m%d)
size=$ (DU-SH/ETC)
if [-d/tmp/dbback]
Then
echo "Date is: $date" >/tmp/dbback/db.txt
echo "Size is: $size" >>/tmp/dbback/db.txt
Cd/tmp/dbback
TAR-ZCF etc_$date.tar.gz/etc db.txt $>/dev/null
Rm-rf/tmp/dbback/db.txt
Else
Mkdir/tmp/dbback
echo "Date is: $date" >/tmp/dbback/db.txt
echo "Size is: $size" >>/tmp/dbback/db.txt
Cd/tmp/dbback
TAR-ZCF etc_$date.tar.gz/etc db.txt $>/dev/null
Rm-rf/tmp/dbback/db.txt
Fi
Example 2: Determine if Apache is booting
#!/bin/bash
port=$ (nmap-st 192.168.3.207 | grep tcp | grep http | awk ' {print $} ')
#使用nmap命令扫描服务器, and intercepts the status of the Apache server, assigning the variable port
If ["$port" = = "Open"]
Then
echo "$ (date) HTTP is ok" >>/tmp/httpd_acc.log
Else
/ETC/RC.D/INIT.D/HTTPD Restart &>/dev/null
echo "$ (date) HTTP is reboot!!" >>/tmp/httpd_err.log
Fi
07. Multi-Branch if condition statement
If [conditional statement 1]
Then
Executing the program 1
elif [Conditional Statement 2]
Then
Executing the program 2
Else
Executing the program 2
Fi
08. Case Statement
Example:
Linux conditional judgment-if statement-case statement-for statement