您的位置:首页 > 理论基础 > 计算机网络

shell作业之http交互脚本

2018-12-29 14:48 375 查看

作业

问:编写httpd监控脚本,要求可以输入start|stop|restart|status

答:脚本如下

[code]#!/bin/bash
#Date:2018-12-28
#Author:nelws-lcz
#Connect:892730644@qq.com
#Desc:This script is for http
#Version:1.0
while true
do
echo -e "
\033[31m A 开启httpd,    \033[0m
\033[32m B 停止httpd      \033[0m
\033[33m C 重启httpd,    \033[0m
\033[34m D 查看httpd状态  \033[0m
\033[35m Q 退出此程序     \033[0m

"
read -p "请输入你的选择:" char

state=`systemctl status httpd | grep "Active" | awk '{print $2}'`
state1=`systemctl status httpd | grep "Loaded" | awk '{print $2}'`

case $char in

a|A)
if [ $state == "inactive" ];then
if [ $state1 == "not-found" ];then
yum install httpd -y >/dev/null
firewall-cmd --add-service=http >/dev/null
firewall-cmd --reload >/dev/null
systemctl start httpd
echo "httpd service is installed and running"
else
systemctl start httpd
firewall-cmd --add-service=http >/dev/null
firewall-cmd --reload >/dev/null
echo "httpd service is running"
fi
else
echo "httpd service is already running"
fi
;;

b|B)
if [ $state == "inactive" ];then
echo "httpd service is already stopped"
else
systemctl stop httpd
echo "httpd service has stopped"
fi
;;

c|C)
if [ $state == "inactive" ];then
echo "httpd service is not running,what you can do is use command A to start it!"
else
systemctl restart httpd
echo "httpd service has been restarted"
fi
;;

d|D)
if [ $state == "inactive" ];then
echo "httpd service is not running"
else
echo "httpd service is running"
fi
;;
q|Q)
exit 0
;;
*)
echo "sorry,you command is not exist,please check!"
esac
done

测试结果

1.http开启选项(a选项)

1)当http已经开启的时候

2)当http没有开启的时候

3)当http没有开启也没有安装的时候

2.http停止选项

1)http已经开启的时候

2)http已经关闭的时候

3.http重启选项

1)http已经开启的时候

2)http已经关闭的时候

4.http状态选项

1)http已经开启的时候

2)http已经关闭的时候

5.离开选项

6.输入无效命令的时候

bingo~

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: