任务
通过编写shell脚本,输入对应的数字实现对应软件的安装;实现多级菜单的效果
实现效果
实现代码:
1、编写脚本
[root@server ~]# vim multi_menu.sh #脚本名称任定义
2、编写代码
#!/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 + 1 &> /dev/null
if [ $? -ne 0 ]
then
echo "#########################"
echo " Warning!!! input error "
echo "Please enter choose[1-4]:"
echo "#########################"
sleep 1
else
if [ $num2 -gt 4 ]
then
echo "#########################"
echo " Warning!!! Out of 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 mysql -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 "\033[031mYour 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)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_lnmp options[1-4]:" num3
expr $num3 + 1 &> /dev/null
if [ $? -ne 0 ]
then
echo "#########################"
echo "Warning!!! input error "
echo "Please enter choose[1-4]:"
echo "#########################"
sleep 1
else
if [ $num3 -gt 4 ]
then
echo "#########################"
echo "Warning!!! Out of range "
echo "Please enter choose[1-4]:"
echo "#########################"
sleep 1
fi
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
clear
lnmp_menu
;;
3)
yum install php -y &> /dev/null
if(($?==0))
then
echo "安装php成功"
fi
sleep 2
clear
lnmp_menu
;;
4)
clear
menu
;;
*)
clear
echo
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 + 1 &> /dev/null
if [ $? -ne 0 ]
then
echo "#########################"
echo "| Warning!!! |"
echo "| Please Enter Right choice!|"
echo "#########################"
sleep 1
elif [ $num1 -gt 4 ]
then
echo "#########################"
echo "| Warning!!! |"
echo "| Out of range! |"
echo "#########################"
else
case $num1 in
1)
clear
lamp_menu
;;
2)
clear
lnmp_menu
;;
3)
clear
break
;;
4)
clear
menu
;;
*)
clear
menu
;;
*)
clear
echo -e "\033[31mYour Enter a number Error,please Enter again choice:[1-4]:\033[0m"
menu
esac
fi
done