实现功能
提供图形化菜单界面
支持常见软件分类(lamp,lnmp)
新建menu.sh脚本
bash
#!/binbash
function menu(){
cat << EOF
----------------------------------------------
|*******Please Enter Your Choice:[1-4]*******|
----------------------------------------------
* `echo -e "\033[35m 1)lamp install\033[0m"`
* `echo -e "\033[35m 2)lnmp install\033[0m"`
* `echo -e "\033[35m 3)quit\033[0m"`
* `echo -e "\033[35m 4)return main menu\033[0m"`
EOF
}
function lamp_menu(){
cat << EOF
----------------------------------------------
|*******Please Enter Your Choice:[1-4]*******|
----------------------------------------------
* `echo -e "\033[35m 1)http install\033[0m"`
* `echo -e "\033[35m 2)mysql install\033[0m"`
* `echo -e "\033[35m 3)php install\033[0m"`
* `echo -e "\033[35m 4)return main menu\033[0m"`
EOF
read -p "####please input second_lamp optios[1-4]:" num2
expr $num2 + 1 &> /dev/null
if [ $? -ne 0 ]
then
echo "#############################"
echo "waing !!!,input error "
echo "please enter choose[1-4]:"
echo "#############################"
sleep 1
else
if [ $num2 -gt 4 ]
then
echo "############################"
echo "waing !!!,out df range"
echo "please enter choose[1-4]:"
echo "############################"
sleep 1
fi
fi
case $num2 in
1)
yum install httpd -y &> /dev/null
if (($?==0))
then
echo "httpd安装成功"
fi
sleep 2
lamp_menu
;;
2)
yum install mariadb-server -y &> /dev/null
if (($?==0))
then
echo "mysql安装成功"
fi
sleep 2
lamp_menu
;;
3)
yum install php* -y &> /dev/null
if (($?==0))
then
echo "php安装成功"
fi
sleep 2
lamp_menu
;;
4)
clear
menu
;;
*)
clear
echo
echo -e "Wring!!!Please input aging choice:[1-4]"
lamp_menu
esac
}
function lnmp_menu(){
cat << EOF
----------------------------------------------
|*******Please Enter Your Choice:[1-4]*******|
----------------------------------------------
* `echo -e "\033[35m 1)nginx install\033[0m"`
* `echo -e "\033[35m 2)mysql install\033[0m"`
* `echo -e "\033[35m 3)php install\033[0m"`
* `echo -e "\033[35m 4)return main menu\033[0m"`
EOF
read -p "####please input second_lamp optios[1-4]:" num2
expr $num2 + 1 &> /dev/null
if [ $? -ne 0 ]
then
echo "#############################"
echo "waing !!!,input error "
echo "please enter choose[1-4]:"
echo "#############################"
sleep 1
else
if [ $num2 -gt 4 ]
then
echo "############################"
echo "waing !!!,out df range"
echo "please enter choose[1-4]:"
echo "############################"
sleep 1
fi
fi
case $num2 in
1)
yum install nginx -y &> /dev/null
if (($?==0))
then
echo "httpd安装成功"
fi
sleep 2
lnmp_menu
;;
2)
yum install mariadb-server -y &> /dev/null
if (($?==0))
then
echo "mysql安装成功"
fi
sleep 2
lnmp_menu
;;
3)
yum install php* -y &> /dev/null
if (($?==0))
then
echo "php安装成功"
fi
sleep 2
lnmp_menu
;;
4)
clear
menu
;;
*)
clear
echo
echo -e "Wring!!!Please input aging choice:[1-4]"
lnmp_menu
esac
}
clear
menu
while true
do
read -p "###please enter your first_menu choice:[1-4]" num1
expr $num1 + 1 &> /dev/null
if (($?>0))
then
echo "-----------------------------"
echo "| waring!!! |"
echo "|please enter right choice! |"
echo "-----------------------------"
sleep 1
elif (($num1>4))
then
echo "-----------------------------"
echo "| waring!!! |"
echo "| out of range! |"
echo "-----------------------------"
sleep 1
else
case $num1 in
1)
clear
lamp_menu
;;
2)
clear
lnmp_menu
;;
3)
clear
break
;;
4)
clear
menu
;;
*)
clear
echo -e "error!!please enter again choice:[1-4]"
menu
esac
fi
done
脚本执行
bash
bash menu.sh
执行结果


小结
本文介绍了一个基于Bash脚本的图形化菜单系统,用于自动化安装LAMP和LNMP环境。脚本包含三个主要功能:主菜单显示LAMP/LNMP安装选项,二级菜单提供具体组件(如HTTP/Nginx、MySQL、PHP)的安装选择。通过case语句处理用户输入,使用yum命令自动安装软件包,并包含输入验证和错误处理功能。执行方式为运行"bash menu.sh",该脚本为用户提供了便捷的服务器环境搭建工具,支持返回主菜单和退出功能。