shell脚本实现多级菜单的编写

一.编写shell脚本

[root@server ~]# cat multi_menu.sh
#!/bin/bash
function menu(){
cat << EOF
-----------------------------------------------
***********input your choice:(1-4)*************
-----------------------------------------------
* `echo -e "\e[32m 1>LAMP install\e[0m"`
* `echo -e "\e[32m 2>LNMP install\e[0m"`
* `echo -e "\e[32m 3>quit\e[0m"` 
* `echo -e "\e[32m 4>return main menu\e0m"`
EOF
}

function lamp_menu(){
cat << EOF
-------------------------------------------------
***********input your choice:(1-4)**************
-------------------------------------------------
* `echo -e "\e[35m 1>http install\e[0m"`
* `echo -e "\e[35m 2>mysql install\e[0m"`
* `echo -e "\e[35m 3>php install\e[0m"`
* `echo -e "\e[35m 4>return main menu\e[0m"`
EOF
read -p "***please input options(1-4):" num
expr $num + 1 &> /dev/null
if [ $? -ne 0 ]
then
	echo "**********************************"
	echo "waing!!!,input error   "
	echo "**********************************"
	sleep 1
else
	if [ $num -gt 4 ]
	then
        	echo "**********************************"
                echo "waing!!!,input error   "
                echo "please enter your choice(1-4):"
                echo "**********************************"
                sleep 1
	fi
fi
case $num in
	1)
		yum install httpd -y &> /dev/null
		if(($?==0))
		then
			echo "安装http成功"
		fi
		sleep 2
		lamp_menu
		;;
	2)
		yum install mariadb-server -y &> /dev/null
		if(($?==0))
		then
			echo "数据库mariadb-service安装成功"
		fi
		sleep 2
		lamp_menu
		;;
	3)
		yum install php-fpm -y &> /dev/null
	        if(($?==0))
		then
			echo "php-fpm安装成功"
		fi
		sleep 2
		lamp_menu
		;;
	4)
		clear
                menu
		;;
	*)
		clear
		echo -e "\033[31m ERROR,please input again(1-4)\033[0m"
		lamp_menu
	esac
}


function lnmp_menu(){
cat << EOF
-------------------------------------------------
***********input your choice:(1-4)**************
-------------------------------------------------
* `echo -e "\e[32m 1>nginx install\e[0m"`
* `echo -e "\e[32m 2>LNMP install\e[0m"`
* `echo -e "\e[32m 3>quit\e[0m"` 
* `echo -e "\e[32m 4>return main menu\e0m"`
EOF

read -p "***please input options(1-4):" num2
expr $num2 + 1 &> /dev/null
if [ $? -ne 0 ]
then
        echo "**********************************"
        echo "waing!!!,input error   "
        echo "**********************************"
        sleep 1
else
        if [ $num2 -gt 4 ]
	then
                echo "**********************************"
                echo "waing!!!,input error   "
                echo "please enter your choice(1-4):"
                echo "**********************************"
                sleep 1
        fi
fi
case $num2 in
        1)
                yum install nginx -y &> /dev/null
                if(($?==0))
                then
                        echo "安装nginx成功"
                fi
                sleep 2
                lnmp_menu
                ;;
        2)
                yum install mariadb-server -y &> /dev/null
                if(($?==0))
		then
                        echo "数据库mariadb-service安装成功"
                fi
                sleep 2
                lnmp_menu
                ;;
        3)
                yum install php-fpm -y &> /dev/null
                if(($?==0))
		then
                        echo "php-fpm安装成功"
                fi
                sleep 2
                lnmp_menu
                ;;
        4)
                clear
                menu
                ;;
        *)
                clear
                echo -e "\033[31m ERROR,please input again(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 + 1 &>/dev/null
	if [ $? -ne 0 ]
	then
		echo "-----------------------------"
                echo "|  please input again        |"
		echo "------------------------------"
		sleep 1
	elif [ $num1 -gt 4 ]
	then
		echo "-----------------------------"
                echo "|     waring!!!              |"
                echo "|  please input again        |"
                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 ERROR,please input again(1-4)\033[0m"
                                 menu
                       esac
      fi
done

二.相关细节

1.read -p "please input a number:" a

expr $a + 0 &>/dev/null    #如果是整数则$a+0返回状态为0,否则为非0

[ $? -eq 0 ] && echo int || echo chars   #通过状态来判断

2.linux shell的sleep指定延时单位

sleep 1 表示默认延时一秒

sleep 1s 表示延迟一秒

sleep 1m 表示延迟一分钟

sleep 1h 表示延迟一小时

sleep 1d 表示延迟一天

3.[root@server ~]# vim color.sh
echo -e "\e[30m 黑色字\e[0m"
echo -e "\e[1;31m 紅色字\e[0m"
echo -e "\e[32m 綠色字\e[0m"
echo -e "\e[33m 黃色字\e[0m"
echo -e "\e[34m 藍色字\e[0m"
echo -e "\e[35m 紫色字\e[0m"
echo -e "\e[36m 天藍字\e[0m"
echo -e "\e[37m 白色字\e[0m"
echo -e "\e[40;37m 黑底白字\e[0m"
echo -e "\e[41;37m 紅底白字\e[0m"
echo -e "\e[42;37m 綠底白字\e[0m"
echo -e "\e[43;37m 黃底白字\e[0m"
echo -e "\e[44;37m 藍底白字\e[0m"
echo -e "\e[45;37m 紫底白字\e[0m"
echo -e "\e[46;37m 天藍底白字\e[0m"
echo -e "\e[47;30m 白底黑字\e[0m"

三.运行结果

 

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁小木在努力冲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值