主菜单
http和Nginx分别的install的菜单,安装过程通过重定向到/dev/null达到看不见的效果
输入非整数或者大于4的数字都会提示错误
代码如下:
[root@server ~]# vim install_menu.sh
#!/bin/bash
function menu()
{
cat << EOF #将内容显示到屏幕上
-----------------------------------------
|***************请输入选项**************|
-----------------------------------------
*$(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 #将内容显示到屏幕上
-----------------------------------------
|***************请输入选项**************|
-----------------------------------------
*$(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 "####请输入子菜单lamp_menu的选项:" num2
expr $num2 + 1 &>/dev/null #expr只支持整数运算,用于判断输入的是不是整数
if [ $? -ne 0 ]
then #如果状态码不为0,代表输入不为整数
echo "#################################"
echo "输入错误,请输入整数"
echo "#################################"
sleep 1 #等待一秒
elif [ $num2 -gt 4 ] #如果输入选项大于4
then
echo "#################################"
echo "输入错误,选项超出范围"
echo "#################################"
sleep 1 #等待一秒
fi
case $num2 in
1)
yum install httpd -y &> /dev/null
if(($?==0))
then
echo "安装httpd成功"
fi
sleep 2
lamp_menu #返回二级菜单
;;
2)
yum install mysql -y &>/dev/null
if(($?==0))
then
echo "安装mysql成功"
fi
sleep 2
lamp_menu #返回二级菜单
;;
3)
yum install *php -y &>/dev/null #php有100多个小的子项,建议直接一起安装
if(($?==0))
then
echo "安装php成功"
fi
sleep 2
lamp_menu #返回二级菜单
;;
4)
clear
menu #返回主菜单
;;
*)
clear
echo #输出换行符
echo -e "\033[31m输入错误选项,请重新输入\033[0m"
lamp_menu
esac
}
function lnmp_menu()
{
cat << EOF #将内容显示到屏幕上
-----------------------------------------
|***************请输入选项**************|
-----------------------------------------
*$(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 "####请输入子菜单lamp_menu的选项:" num3
expr $num3 + 1 &> /dev/null #expr只支持整数运算,用于判断输入的是不是整数
if [ $? -ne 0 ] #如果上一条命令执行成功则状态码$?是0
then
echo "#################################"
echo "输入错误,请输入整数"
echo "#################################"
sleep 1 #等待一秒
elif [ $num3 -gt 4 ] #如果输入选项大于4
then
echo "#################################"
echo "输入错误,选项超出范围"
echo "#################################"
sleep 1 #等待一秒
fi
case $num3 in
1)
yum install nginx -y &> /dev/null
if(($?==0))
then
echo "安装nginx成功"
fi
sleep 2
lnmp_menu #返回二级菜单
;;
2)
yum install mysql -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 "\033[31m输入错误选项,请重新输入\033[0m"
lnmp_menu #返回二级菜单
esac
}
clear
menu
while true
do
read -p "请在主菜单输入选项:" num1
expr $num3 + 1 &> /dev/null #expr只支持整数运算,用于判断输入的是不是整数
if [$? -ne 0] #如果上一条命令执行成功则状态码$?是0
then
echo "#################################"
echo "输入错误,请输入整数"
echo "#################################"
sleep 1 #等待一秒
elif [ $num3 -gt 4] #如果输入选项大于4
then
echo "#################################"
echo "输入错误,选项超出范围"
echo "#################################"
sleep 1 #等待一秒
else
case $num1 in
1)
clear
lamp_menu
;;
2)
clear
lnmp_menu
;;
3)
clear
break
;;
4)
clear
menu
;;
*)
clear
echo -e "\033[31m输入错误选项,请重新输入\033[0m"
menu
esac
fi
done