SHELL综合项目

本文介绍了一个使用Bash编写的脚本,实现LAMP和LNMP环境的多级菜单安装选项,无需LAMP/LNMP环境,可根据需求进行简单修改。脚本演示了如何安装HTTPD、MySQL和PHP,以及注意事项和case语句的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.前提

本脚本主要实现多级菜单效果,并没有安装LAMP、LNMP环境,如果要用在实际生成环境中部署LNMP、LAMP环境,只需要简单修改一下就可以了。

2.演示效果

3.参考代码

#!/bin/bash

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 options[1-4]:" num2
expr $num2 + 2 &> /dev/null
if [ $? -ne 0 ]
then
     echo "#########################"
     echo "waring!!!,input error    "
     echo "please enter choose[1-4]:"
     echo "#########################"
     sleep 1
elif [ $num2 -gt 4 ]
then 
     echo "#########################"
     echo "waring!!!,Out of range   "
     echo "please enter choose[1-4]:"
     echo "#########################"
     sleep 1
fi
case $num2 in
	1)
		yum install httpd -y &>/dev/null
		echo "安装httpd成功"
		sleep 2
		clear
		lamp_menu
		;;
	2)
		yum install mysql -y &>/dev/null
		echo "安装mysql成功"
		sleep 2
		clear
		lamp_menu
		;;
	3)
		yum install PHP -y &>/dev/null
		echo "安装PHP成功"
		sleep 2
		clear
		lamp_menu
		;;
	4)
                clear
		menu
		;;
	5)              
		clear
                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_lamp options[1-4]:" num3
expr $num3 + 3 &>/dev/null
if [ $? -ne 0 ]
then
     echo "#########################"
     echo "waring!!!,input error    "
     echo "please enter choose[1-4]:"
     echo "#########################"
     sleep 1
elif [ $num3 -gt 4 ]
then
     echo "#########################"
     echo "waring!!!,Out of range   "
     echo "please enter choose[1-4]:"
     echo "#########################"
     sleep 1
fi
case $num3 in
        1)
                yum install Nginx -y &>/dev/null
                echo "安装Nginx成功"
                sleep 2
		clear
                lnmp_menu
                ;;
        2)
                yum install mysql -y &>/dev/null
                echo "安装mysql成功"
                sleep 2
		clear
                lnmp_menu
                ;;

        3)
                yum install PHP -y &>/dev/null
                echo "安装PHP成功"
                sleep 2
		clear
                lnmp_menu
                ;;
        4)
                clear
                menu
		;;
	*)
		clear
		echo -e "\033[31mYour Enter the wrong,Please input again Choice:[1-4]\033[0m"
		lnmp_menu
 esac
}


clear
menu
while true
do
    read -p "##please enter your first_menu choice[1-4]:" num1
    expr $num1 + 2 &> /dev/null 
    if [ $? -ne 0 ]
    then
	echo "----------------------------"
	echo "|         Waring!!!        |"
	echo "|Please Enter Right Choice!|"
	echo "----------------------------"
   	sleep 1
    elif [ $num1 -gt 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
	esac
	fi
done

4.注意事项:

注意case用法:

        适用于变量有多种取值

        case语句只是判断一个变量的不同取值,而if需要判断多个不同的条件

        整个分支结构包括在case…esac 之间,中间的模式1,模式2,…,*对应为变量不同取值,其中星号作为通配符,可匹配所有值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值