1.编写menu函数,后续被调用。
#!/bin/bash
#menu.sh
function menu() {
time=`date '+%Y-%m-%d %T'`
cat << eof
####################################
Menu
####################################
* 1) stop apache, mysql
* 2) start apache, mysql
* 3) shutdown system
* 4) quit
####################################
$time
####################################
eof
}
2.输入选项关闭相应服务,如Apache、Mysql
#!/bin/bash
#mangeLAMP
. menu.sh
clear
menu
while true
do
read -p "please input your choice:" option
case $option in
1)
service httpd stop
if [ $? -eq 0 ]; then
echo "httpd is stoped"
else
echo "httpd is not stoped"
fi
service mysql stop
if [ $? -eq 0 ]; then
echo "mysql is stoped"
else
echo "mysql is not stoped"
fi
;;
2)
service httpd start
if [ $? -eq 0 ]; then
echo "httpd is started"
else
echo "httpd is not started"
fi
service mysql start
if [ $? -eq 0 ]; then
echo "mysql is started"
else
echo "mysql is not started"
fi
;;
3)
poweroff
echo "The system is poweroff...."
;;
4)
break
;;
*)
echo "Caution! Please input choice: 1~4!!"
;;
esac
done
3.测试sh manageLAMP.sh
####################################
Menu
####################################
* 1) stop apache, mysql
* 2) start apache, mysql
* 3) shutdown system
* 4) quit
####################################
2018-09-17 22:38:39
####################################
please input your choice:1
Stopping httpd: [ OK ]
httpd is stoped
Shutting down MySQL.. SUCCESS!
mysql is stoped
please input your choice:4
[root@REDHAT html]#
####################################
Menu
####################################
* 1) stop apache, mysql
* 2) start apache, mysql
* 3) shutdown system
* 4) quit
####################################
2018-09-17 22:40:35
####################################
please input your choice:2
Starting httpd: [ OK ]
httpd is started
Starting MySQL. SUCCESS!
mysql is started
please input your choice:4
[root@REDHAT html]#