shell作业之http交互脚本

作业

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

答:脚本如下

#!/bin/bash
#Date:2018-12-28
#Author:nelws-lcz
#Connect:[email protected]
#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~

猜你喜欢

转载自blog.csdn.net/weixin_40543283/article/details/85338629
今日推荐