if语句
if [ 条件判断是];then 空格不能省略
程序
fi
或者
if [ 条件判断式]
then 程序
fi
需要注意的是:
1.if 开头,fi 结尾
例子
if [$rate -ge 80]
then
echo "warning,/dev/sda3 is full!!"
fi
双if语句
if [ 条件判断]
then dosth
else
do sth
eg:备份Mysql数据库
#!/bin/bash
#同步系统时间
date=$(date+%y%m%d)
#统计数据库大小
size=$(du -sh /var/lib/mysql)
if [ -d /tmp/dbbak]
then
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
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
rm -rf /tmp/dbbak/dbinfo.txt