打印选择菜单,一键安装Web服务:

[root@oldboyscripts]# sh menu.sh 
    1.[install lamp]
    2.[install lnmp]
    3.[install mysql]
    4.[install php]
    5.[exit]
    pls input the num you want:

要求:

1、当用户输入1时,输出“startinstallinglamp.”然后执行/server/scripts/lamp.sh,脚本内容输出"lampis installed"后退出脚本;

2、当用户输入2时,输出“startinstallinglnmp.”然后执行/server/scripts/lnmp.sh输出"lnmpis installed"后退出脚本;

3、当用户输入2时,输出“startinstallingmysql.”然后执行/server/scripts/mysql.sh输出"mysql installed"后退出脚本;

4、当用户输入2时,输出“startinstallingphp.”然后执行/server/scripts/php.sh输出"php installed"后退出脚本;

5、当输入3时,退出当前菜单及脚本;

6、当输入任何其它字符,给出提示“Input error”后退出脚本。

7、要对执行的脚本进行相关条件判断,例如:脚本是否存在,是否可执行等。

解答:

#!/bin/bash
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
PINK_COLOR='\E[1;35m'
RES='\E[0m'
cat <<EOF   要打印的菜单
1.[install lamp]
2.[install lnmp]
3.[install mysql]
4.[install php]
5.[exit]
EOF
read -p"pls input the num you want:" a   请输入一个参数
case $a in
1)
  echo -e "$BLUE_COLOR startinstalling lamp $RES"    给输出的内容加上颜色
  lampScripts=/server/scripts/lamp.sh
 [-f$lampScripts] && sh $lampScripts|| exit1   判断要执行的lamp文件是否存在
 ;;
 2)
  echo -e "$PINK_COLOR startinstalling lnmp $RES"
  lnmpScripts=/server/scripts/lnmp.sh
  [-f$lnmpScripts] && sh $lnmpScripts|| exit2
 ;;
 3)
  echo -e "$GREEN_COLOR startinstalling mysql $RES"
  mysqlScripts=/server/scripts/mysql.sh
  [-f$mysqlScripts] && sh $mysqlScripts|| exit3
 ;;
 4)
 echo-e "$PINK_COLOR startinstalling php $RES"
 phpScripts=/server/scripts/mysql.sh
  [-f$phpScripts] && sh $phpScripts|| exit4
  ;;
 *)
  echo -e "$RED_COLOR input error $RES"
esac