Shell项目实践—多级菜单脚本编写
1. 脚本的解释
1.1 脚本概述
- 这是一个用Bash编写的多级菜单脚本;
- 主要用于演示LAMP和LNMP环境的安装流程;
- 实际不执行安装操作,但可以轻松修改为真实安装脚本。
1.2 主要功能模块
function menu() function lamp_menu() function lnmp_menu()
1.3 工作流程
- 脚本启动后显示主菜单,提供4个选项:
-
- LAMP安装 (Apache+MySQL+PHP)
-
- LNMP安装 (Nginx+MySQL+PHP)
-
- 退出
-
- 返回主菜单
1.4 输入验证机制
- 使用
expr $num + 1
验证输入是否为整数 - 检查数字是否在有效范围内(1-4)
- 无效输入会显示警告并重新显示菜单
1.5 安装模拟功能
- 使用
yum install
命令模拟软件包安装 - 安装结果通过
$?
检查 - 成功安装后显示中文提示信息
1.6 界面设计
- 使用
echo -e "\033[35m"
实现紫色文本 - 使用
clear
命令保持界面整洁 - 菜单结构清晰,使用分隔线增强可读性
1.7 递归菜单控制
- 主循环使用
while true
实现持续运行 - 通过
break
语句退出脚本 - 子菜单通过递归调用实现
2. 脚本内容
2.1 代码如下
#!/bin/bash
1. 本脚本主要实现多级菜单效果,并没有安装LAMP,LNMP环境。
2. 如果要用在实际生产环境中部署LNMP,LNMP环境,只需要简单修改一下即可。
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
elseif [ $num2 -gt 4 ]thenecho "###########################"echo "Waing !!!,Out of range "echo "Please enter choose[1-4]:"echo "##########################"sleep 1fi
fi
case $num2 in1)yum install httpd -y &> /dev/nullif(($?==0))thenecho "安装httpd成功"fisleep 2lamp_menu;;2)yum install mysql -y &> /dev/nullif(($?==0))thenecho "安装mysql成功"fisleep 2lamp_menu;;3)yum install php* -y &> /dev/nullif(($?==0))thenecho "安装php成功"fisleep 2lamp_menu;;4)clearmenu;;*)clearecho echo -e "\033[31mYour Enter the wrong,Please input again Choice:[1-4]\033[0m"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_lnmp options[1-4]: " num3
expr $num3 + 1 &>/dev/null
if [ $? -ne 0 ]
then echo "###########################"echo "Waing !!!,input error "echo "Please enter choose[1-4]:"echo "##########################"sleep 1
elseif [ $num3 -gt 4 ]thenecho "###########################"echo "Waing !!!,Out of range "echo "Please enter choose[1-4]:"echo "##########################"sleep 1fi
fi
case $num3 in1)yum install nginx -y &> /dev/nullif(($?==0))thenecho "安装nginx成功"fisleep 2lnmp_menu;;2)yum install mysql -y &> /dev/nullif(($?==0))thenecho "安装mysql成功"fisleep 2clearlnmp_menu;;3)yum install php -y &> /dev/nullif(($?==0))thenecho "安装php成功"fisleep 2clearlnmp_menu;;4)clearmenu;;*)clearechoecho -e "\033[31mYour Enter the wrong,Please input again Choice:[1-4]\033[0m"lnmp_menu
esac
}clear
menu
while true
doread -p "##please Enter Your first_menu Choice:[1-4] " num1expr $num1 + 1 &>/dev/null if [ $? -ne 0 ]then echo "----------------------------"echo "| Waring!!! |"echo "|Please Enter Right Choice!|"echo "----------------------------"sleep 1elif [ $num1 -gt 4 ]then echo "----------------------------"echo "| Waring!!! |"echo "| Out of range! |"echo "----------------------------"sleep 1elsecase $num1 in1)clearlamp_menu;;2)clearlnmp_menu;;3)clearbreak;;4)clearmenu;;*)clearecho -e "\033[31mYour Enter a number Error,Please Enter again Choice:[1-4]: \033[0m" menuesacfi
done
2.2 界面演示如下
2.2.1 主界面

2.2.2 LAMP界面

2.2.3 LNMP界面