(1)单分支结构
if 条件判断;then
fi
#!/bin/bash
read -p "please input Y" num
if [ "$num" != Y ];then
echo "error input"
exit
fi
(2)双分支结构
if 条件判断;then
命令
else
命令
fi
#!/bin/bash
ping -c1 www.baidu.com
if [ $? -eq 0 ];then
yum install httpd -y
systemctl start httpd.service
systemctl stop httpd.service
else
echo "please check network"
fi
(3)多分支
if 条件判断;then
命令
elif 条件判断1;then
命令
else
命令
fi
#!/bin/bash
#根据不同的centos版本下载不同的yum源
os_version=$(cat /etc/redhat-release | awk '{print $4}' | awk -F"." '{print $1}')
[ -d /etc/yum.repos.d ] || mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
if [ $os_version -ge 7 ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
elif [ $os_version -ge 6 -a $os_version -lt 7 ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum clean all
yum makecache
elif [ $os_version -ge 5 -a $os_version -lt 6 ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
yum clean all
yum makecache
else
echo "os is not centos version!"
fi