一:条件测试操作
1:文件测试
[root@localhost ~]# [ -d /media/cdrom ] \\-d,测试目录是否存在
[root@localhost ~]# echo $?
0
[root@localhost ~]# [ -d /media/cdrom/Server ]
[root@localhost ~]# echo $?
0
[root@localhost ~]# [ -d /media/cdrom/server ]
[root@localhost ~]# echo $?
1
[root@localhost ~]# [ -d /media/cdrom/server ] && echo "yes" \\无输出表示不存在
[root@localhost ~]# [ -d /media/cdrom/Server ] && echo "yes"
yes
2:整数值比较
[root@localhost ~]# unum=`who | wc -l`
[root@localhost ~]# [ $unum -gt 5 ] && echo "Too many"
[root@localhost ~]# [ $unum -gt 0 ] && echo "Too many"
Too many
[root@localhost ~]# freecc=$(free -m | grep "Mem" | awk '{print $4+$6}') \\可用内存空间
[root@localhost ~]# [ $freecc -lt 2048 ] && echo ${freecc}MB \\小于某值后输出具体的值
1269MB
3:字符串比较
[root@localhost ~]# echo $LANG \\当前的语言环境
zh_CN.UTF-8
[root@localhost ~]# [ LANG != "en.US" ] && echo "not en.US" \\字符串比较测试 !=前后有空格
not en.US
[root@localhost ~]# read -p "是否覆盖现有文件(yes/no)?" ACK
是否覆盖现有文件(yes/no)?yes
[root@localhost ~]# [ $ACK = "yes" ] && echo "覆盖"
覆盖
[root@localhost ~]# read -p "是否覆盖现有文件(yes/no)?" ACK
是否覆盖现有文件(yes/no)?no
[root@localhost ~]# [ $ACK = "no" ] && echo "不覆盖"
不覆盖
4:逻辑测试
[root@localhost ~]# uname -r
3.10.0-514.el7.x86_64
[root@localhost ~]# Mnum=$(uname -r | awk -F. '{print $1}')
[root@localhost ~]# Snum=$(uname -r | awk -F. '{print $2}')
[root@localhost ~]# [ $Mnum -eq 3 ] && [ $Snum -gt 4 ] && echo "符合要求"
符合要求
二:使用if条件语句
1:单分支if语句
[root@localhost ~]# vim chkmountdir.sh \\如果挂载点不存在,自动创建
#!/bin/bash
MOUNT_DIR="/media/cdrom"
if [ ! -d $MOUNT_DIR ]
then
mkdir -p $MOUNT_DIR
fi
[root@localhost ~]# chmod +x chkmountdir.sh
[root@localhost ~]# ./chkmountdir.sh
[root@localhost ~]# vi /opt/chkifroot.sh \\判断是否是root,如果不是,没有权限操作
#!/bin/bash
if [ "$USER" != "root" ]
then
echo "错误:非root用户,权限不足!"
exit 1
fi
fdisk -l /dev/sda
[root@localhost ~]# chmod +x /opt/chkifroot.sh
[root@localhost ~]# /opt/chkifroot.sh \\root可以执行
[aaa@localhost root]$ /opt/chkifroot.sh \\其他用户不可以执行
2:双分支if语句应用
[root@localhost ~]# vim chkmountdir.sh
#!/bin/bash
MOUNT_DIR="/media/cdrom"
if [ ! -d $MOUNT_DIR ]
then
mkdir -p $MOUNT_DIR
else
mount /dev/cdrom $MOUNT_DIR
fi
[root@localhost ~]# vi pinghost.sh \\判断另一主机是否在线
#!/bin/bash
ping -c 3 -i 0.2 -W 3 $1 &> /dev/null
if [ $? -eq 0 ]
then
echo "Host $1 is up."
else
echo "Host $1 is down."
fi
[root@localhost ~]# chmod +x pinghost.sh
[root@localhost ~]# ./pinghost.sh 192.168.10.101
Host 192.168.10.10 is up.
[root@localhost ~]# ./pinghost.sh 192.168.10.1
Host 192.168.10.1 is down.
注释:
-c:ping的次数
-i:ping的间隔
-W:超时时间
[root@localhost ~]# vi chkftpd.sh \\检查vsftpd是否运行
#!/bin/bash
systemctl status vsftpd &>/dev/null
if [ $? -eq 0 ]
then
echo "监听地址:$(netstat -anpt | grep vsftpd | awk '{print $4}')"
echo "进程ID:$(pgrep -x vsftpd)"
else
echo "警告,vsftpd服务不可用"
fi
[root@localhost ~]# chmod +x chkftpd.sh
[root@localhost ~]# ./chkftpd.sh
警告:vsftpd服务不可用!
3:多分支if语句应用
[root@localhost ~]# vim chkmountdir.sh
#!/bin/bash
MOUNT_DIR="/media/cdrom"
if [ ! -d $MOUNT_DIR ]
then
mkdir -p $MOUNT_DIR
mount /dev/cdrom $MOUNT_DIR
elif
[ ! -d /$MOUNT_DIR/Packages ]
then
mount /dev/cdrom /media/cdrom
else
exit 1
fi
[root@localhost ~]# vi gradediv.sh \\检查考试成绩是否合格
#!/bin/bash
read -p "请输入您的分数(0-100):" GRADE
if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ]
then
echo "$GRADE 分,优秀"
elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ]
then
echo "$GRADE 分,合格"
else
echo "$GRADE 分,不合格"
fi
[root@localhost ~]# chmod +x gradediv.sh
[root@localhost ~]# ./gradediv.sh
请输入您的分数(0-100):67
67 分,不合格
[root@localhost ~]# ./gradediv.sh
请输入您的分数(0-100):78
78 分,合格
[root@localhost ~]# ./gradediv.sh
请输入您的分数(0-100):90
90 分,优秀
更多分支
[root@localhost ~]# vi gradediv.sh
#!/bin/bash
read -p "请输入你的成绩" GRADE
if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ]
then
echo "$GRADE分,成绩优秀"
elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ]
then
echo "$GRADE分,合格"
elif [ $GRADE -ge 0 ]&& [ $GRADE -le 69 ]
then
echo "不及格"
else
echo "输入错误"
fi