(4.1)if条件判断 安装apache

文章目录

1.流程控制

  • if用法:条件测试只需要返回真假即可
1if 条件; then       
	命令
fi

等价于:
if	条件			##不用;
then
	命令
fi

(2if 条件; then
	命令
else
	命令

(3if 条件; then
	命令
elif  条件;then
	命令
elif 条件;then
	命令
else 
	命令
fi
	
(4)
read -p "是否确认[y]:" kvm
if [! $kvm = "y" ];then
	echo -e "不正确"
fi

  • eg1:
删除网关:
ip r
ip r del default via 192.168.122.1

#!/usr/bin/bash
#install apache
#v1.0 by wangji 2020-2-19
ping -c1 www.baidu.com &>/dev/null
if [ $? -ne 0];then
	echo "connect: unreachable"
	exit
fi

yum -y install httpd
systemctl start httpd
systemctl enable hrrpd
firewall-cmd --permanent -add-service=http
firewall-cmd --permanent -add-service=https
firewall-cmd reload
sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
setenforce 0
##以SELINUX开头的行,/c表示换成SELINUX=disabled
  • eg2:
#!/usr/bin/bash
#install apache
#v1.0 by wangji 2020-2-19
gateway=192.168.122.1

ping -c1 www.baidu.com &>/dev/null
if [ $? -eq 0];then
	yum -y install httpd
	systemctl start httpd
	systemctl enable hrrpd
	firewall-cmd --permanent -add-service=http
	firewall-cmd --permanent -add-service=https
	firewall-cmd reload
	sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
	setenforce 0
	
	##访问apache
	curl http://127.0.0.1 &>/dev/null
	if [ $? -eq 0 ];then
		echo "Apache OK"
	fi
	
elif ping -c1 $gateway &>/dev/null;then
	echo "check dns"
else
	echo "check ip address "
fi

网关的设置位置: 
cat /etc/resolve.conf


发布了582 篇原创文章 · 获赞 143 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/u011436427/article/details/104401092
今日推荐