编写一个弹出式菜单的shell程序并实现_使用Shell函数实现多级菜单系统安装

最近因为公司项目需求,项目应用也比较多,所以编写一个shell函数多级菜单自动化部署各种应用的脚本:

本脚本实现的功能:

编写一个弹出式菜单的shell程序并实现_使用Shell函数实现多级菜单系统安装_第1张图片

演示效果:

1、一级菜单

编写一个弹出式菜单的shell程序并实现_使用Shell函数实现多级菜单系统安装_第2张图片

2、二级菜单

编写一个弹出式菜单的shell程序并实现_使用Shell函数实现多级菜单系统安装_第3张图片
编写一个弹出式菜单的shell程序并实现_使用Shell函数实现多级菜单系统安装_第4张图片

3、脚本参考

#!/bin/bash

#################################

#

# 单节点部署(centos 7)

# system time ntpdate(配置selinux,防火墙,时间同步,更改时区)

# nginx 1.14.2

# redis 5.0.5

# rabbitmq 3.7.17

# dotnet 2.2.301

# mysql 5.7.27

# pgsql 11.5

#################################

# 双节点部署(centos 7)

# keepalived负载均衡

# redis主从

# rabbitmq集群

# nginx负载均衡(代理两台web应用)

#################################

show_err() { echo -e "[033[31mFAILED033[0m] $1";}

show_ok() { echo -e "[033[32m OK 033[0m] $1";}

show_info() { echo -e "[033[33mNOTICE033[0m] $1";}

# must use root

[ $(id -u) != 0 ] && show_err 'The Script must be run as root.' && exit 1

# must use centos7

if [ -f /etc/centos-release ]; then

[ $(cat /etc/centos-release|awk '{print $4}'|cut -b1) != '7' ] && show_err 'The Script must be run as CentOS 7.' && exit 0

else

show_err 'The Script must be run as CentOS 7.' && exit 0

fi

#系统时间同步

time_ntpdate(){

echo ""

echo -e "033[33m*****system time update*****033[0m"

#general

setenforce 0

sed -i 's@SELINUX=enforcing@SELINUX=disabled@' /etc/selinux/config

systemctl stop firewalld

systemctl disable firewalld

#时间同步

yum install ntpdate -y

ntpdate cn.pool.ntp.org && show_ok 'Ntpdate install is complete!'

echo "*/5 * * * * ntpdate cn.pool.ntp.org" >> /var/spool/cron/root

#更改系统时区

mv /etc/localtime /etc/localtime.bak

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

hwclock -w && show_ok "033[33m*****system time ntpdate is complete!*****033[0m"

echo ""

sleep 5

}

#install nginx

install_nginx(){

echo ""

echo -e "033[33m*****install nginx*****033[0m"

yum install ./rpms/nginx/nginx-1.17.3-1.el7.ngx.x86_64.rpm -y

systemctl enable nginx && show_ok 'NginX install is complete!'

while :; do show_info 'Select template to initialize NginX?:'

echo ' 1) Apollo'

echo ' 0) No need'

read -p 'Use template for NginX [1,0]: ' -r -e nginx_template

[[ ! ${nginx_template} =~ ^[0-1]$ ]] && show_err 'Invalid option! Please only input number 0-1' || break; done

[ ${nginx_template} == '1' ] && /bin/bash ./scripts/nginx_apollo.sh

systemctl restart nginx && show_ok '033[33m*****NginX is startd now!*****033[0m'

echo ""

sleep 5

}

# install redis5.0.5

install_redis(){

echo ""

echo -e "033[33m*****install redis*****033[0m"

yum install ./rpms/redis/redis-5.0.5-1.el7.remi.x86_64.rpm -y

systemctl enable redis.service && show_ok 'Redis install is complete!'

/bin/bash ./scripts/redis.sh

systemctl restart redis.service && show_ok '033[33m*****Redis is startd now!*****033[0m'

echo ""

sleep 5

}

# install rabbitmq

install_rabbitmq(){

echo ""

echo -e "033[33m*****install rabbitmq*****033[0m"

yum install ./rpms/rabbitmq/*.rpm -y

systemctl enable rabbitmq-server.service && show_ok 'RabbitMQ install is complete!'

systemctl restart rabbitmq-server.service

/bin/bash ./scripts/rabbitmq.sh

show_ok '033[33m*****RabbitMQ is startd now!*****033[0m'

echo ""

sleep 5

}

# install dotnet Sdk

install_dotnet_Sdk(){

echo ""

echo -e "033[33m*****install dotnet sdk*****033[0m"

yum install ./rpms/dotnet/*.rpm ./rpms/public/libicu.rpm -y

show_ok '.NetCore SDK install is complete!'

while :; do show_info 'Select template to initialize .NetCore?:'

echo ' 1) Apollo'

echo ' 0) No need'

read -p 'Use template for .NetCore [1,0]: ' -r -e dotnet_template

[[ ! ${dotnet_template} =~ ^[0-1]$ ]] && show_err 'Invalid option! Please only input number 0-1' || break; done

[ ${dotnet_template} == '1' ] && /bin/bash ./scripts/dotnet_apollo.sh

show_ok '033[33m*****dotnet is startd now!*****033[0m'

echo ""

sleep 5

}

# install dotnet Runtime

install_dotnet_Runtime(){

echo ""

echo -e "033[33m*****install dotnet Runtime*****033[0m"

yum install ./rpms/dotnet/*-.rpm ./rpms/public/libicu.rpm -y

show_ok '.NetCore Runtime install is complete!'

while :; do show_info 'Select template to initialize .NetCore?:'

echo ' 1) Apollo'

echo ' 0) No need'

read -p 'Use template for .NetCore [1,0]: ' -r -e dotnet_template

[[ ! ${dotnet_template} =~ ^[0-1]$ ]] && show_err 'Invalid option! Please only input number 0-1' || break; done

[ ${dotnet_template} == '1' ] && /bin/bash ./scripts/dotnet_apollo.sh

show_ok '033[33m*****dotnet is startd now!*****033[0m'

echo ""

sleep 5

}

# install mysql 5.7.27

install_mysql(){

echo ""

echo -e "033[33m*****install mysql*****033[0m"

yum install ./rpms/mysql/*.rpm -y

systemctl enable mysqld.service && show_ok 'MySQL install is complete!'

/bin/bash ./scripts/mysql.sh

systemctl restart mysqld.service && show_ok '033[33m*****MySQL is startd now!*****033[0m'

echo ""

sleep 5

}

#install pgsql 11.5

install_pgsql(){

echo ""

echo -e "033[33m*****install pgsql*****033[0m"

yum install ./rpms/postgresql/*.rpm ./rpms/public/libicu.rpm -y

/bin/bash ./scripts/postgresql.sh

systemctl enable postgresql-11.service && show_ok 'PostgreSQL install is complete!'

#sed -i 's@Environment=PGDATA=/var/lib/pgsql/11/data/@Environment=PGDATA=/data/pgsql/data/@' /usr/lib/systemd/system/postgresql-11.service

systemctl restart postgresql-11.service && show_ok '033[33m*****PostgreSQL is startd now!*****033[0m'

sudo -u postgres psql -U postgres -d postgres -c "alter user postgres with password 'kjfdFJ24jlj';"

echo ""

sleep 5

}

#install keepalived master

install_keepalived_master(){

echo ""

echo -e "033[33m*****install keepalived master*****033[0m"

/bin/bash ./scripts/keepalived_master.sh

systemctl restart keepalived.service && show_ok '033[33m*****keepalived master is startd now!*****033[0m'

echo ""

sleep 5

}

#install keepalived slave

install_keepalived_slave(){

echo ""

echo -e "033[33m*****install keepalived slave*****033[0m"

/bin/bash ./scripts/keepalived_slave.sh

systemctl restart keepalived.service && show_ok '033[33m*****keepalived slave is startd now!*****033[0m'

echo ""

sleep 5

}

#install redis master

install_redis_master(){

echo ""

echo -e "033[33m*****install redis master*****033[0m"

yum install ./rpms/redis/redis-5.0.5-1.el7.remi.x86_64.rpm -y

systemctl enable redis.service && show_ok 'Redis install is complete!'

/bin/bash ./scripts/redis_master.sh

systemctl restart redis.service && show_ok '033[33m*****redis master is startd now!*****033[0m'

echo ""

sleep 5

}

#install redis slave

install_redis_salve(){

echo ""

echo -e "033[33m*****install redis slave*****033[0m"

yum install ./rpms/redis/redis-5.0.5-1.el7.remi.x86_64.rpm -y

systemctl enable redis.service && show_ok 'Redis install is complete!'

/bin/bash ./scripts/redis_slave.sh

systemctl restart redis.service && show_ok '033[33m*****redis slave is startd now!*****033[0m'

echo ""

sleep 5

}

# rabbitmq 集群 host

install_rabbitmq_cluster_host(){

echo ""

echo -e "033[33m*****configuration rabbitmq cluster host*****033[0m"

/bin/bash ./scripts/rabbitmq_cluster_host.sh

show_ok '033[33m*****configuration rabbitmq cluster host is complete!*****033[0m'

echo ""

sleep 5

}

#install rabbitmq node1

install_rabbitmq_node1(){

echo ""

echo -e "033[33m*****install rabbitmq node1*****033[0m"

yum install ./rpms/rabbitmq/*.rpm -y

systemctl enable rabbitmq-server.service && show_ok 'RabbitMQ install is complete!'

systemctl restart rabbitmq-server.service

/bin/bash ./scripts/rabbitmq_node1.sh

show_ok '033[33m*****rabbitmq node1 is startd now!*****033[0m'

echo ""

sleep 5

}

#install rabbitmq node2

install_rabbitmq_node2(){

echo ""

echo -e "033[33m*****install rabbitmq node2*****033[0m"

yum install ./rpms/rabbitmq/*.rpm -y

systemctl enable rabbitmq-server.service && show_ok 'RabbitMQ install is complete!'

systemctl restart rabbitmq-server.service

/bin/bash ./scripts/rabbitmq_node2.sh

show_ok '033[33m*****rabbitmq node2 is startd now!*****033[0m'

echo ""

sleep 5

}

#install nginx load

install_nginx_load(){

echo ""

echo -e "033[33m*****install nginx*****033[0m"

yum install ./rpms/nginx/nginx-1.17.3-1.el7.ngx.x86_64.rpm -y

systemctl enable nginx && show_ok 'NginX install is complete!'

while :; do show_info 'Select template to initialize NginX?:'

echo ' 1) Apollo'

echo ' 0) No need'

read -p 'Use template for NginX [1,0]: ' -r -e nginx_template

[[ ! ${nginx_template} =~ ^[0-1]$ ]] && show_err 'Invalid option! Please only input number 0-1' || break; done

[ ${nginx_template} == '1' ] && /bin/bash ./scripts/nginx_load_apollo.sh

systemctl restart nginx && show_ok '033[33m*****NginX is startd now!*****033[0m'

echo ""

sleep 5

}

#menu2

menu2(){

while true;

do

clear

cat <

----------------------------------------

|****Please Enter Your Choice:[0-8]****|

----------------------------------------

(1) system time ntpdate

(2) Nginx

(3) Redis

(4) rabbitmq

(5) Dotnet Core SDK

(6) Dotnet Core Runtime

(7) Mysql

(8) Pgsql

(0) 返回上一级菜单

EOF

read -p "Please enter your Choice[0-8]: " input2

case "$input2" in

0)

clear

break

;;

1)

time_ntpdate

;;

2)

install_nginx

;;

3)

install_redis

;;

4)

install_rabbitmq

;;

5)

install_dotnet_Sdk

;;

6)

install_dotnet_Runtime

;;

7)

install_mysql

;;

8)

install_pgsql

;;

*) echo "----------------------------------"

echo "| Warning!!! |"

echo "| Please Enter Right Choice! |"

echo "----------------------------------"

for i in `seq -w 3 -1 1`

do

echo -ne "bb$i";

sleep 1;

done

clear

esac

done

}

#menu3

menu3(){

while true;

do

clear

cat <

----------------------------------------

|****Please Enter Your Choice:[0-8]****|

----------------------------------------

(1) keepalived master

(2) keepalived salve

(3) redis master

(4) redis salve

(5) 配置rabbitmq cluster host(两台都要配置并重启系统)

(6) rabbitmq 集群node1

(7) rabbitmq 集群node2

(8) nginx负载均衡(代理两台web应用)

(0) 返回上一级菜单

EOF

read -p "Please enter your Choice[0-8]: " input3

case "$input3" in

0)

clear

break

;;

1)

install_keepalived_master

;;

2)

install_keepalived_slave

;;

3)

install_redis_master

;;

4)

install_redis_salve

;;

5)

install_rabbitmq_cluster_host

;;

6)

install_rabbitmq_node1

;;

7)

install_rabbitmq_node2

;;

8)

install_nginx_load

;;

*) echo "----------------------------------"

echo "| Warning!!! |"

echo "| Please Enter Right Choice! |"

echo "----------------------------------"

for i in `seq -w 3 -1 1`

do

echo -ne "bb$i";

sleep 1;

done

clear

esac

done

}

#initTools

#menu

while true;

do

clear

echo "========================================"

echo ' apollo Optimization '

echo "========================================"

cat << EOF

|****Please Enter Your Choice:[0-2]****|

----------------------------------------

(1) 部署单节点

(2) 部署双节点

(0) 退出Exit

EOF

#choice

read -p "Please enter your choice[0-2]: " input1

case "$input1" in

0)

clear

break

;;

1)

menu2

;;

2)

menu3

;;

*)

echo "----------------------------------"

echo "| Warning!!! |"

echo "| Please Enter Right Choice! |"

echo "----------------------------------"

for i in `seq -w 3 -1 1`

do

echo -ne "bb$i";

sleep 1;

done

clear

esac

done

更多运维技术分享,请关注我“爱踢人生”,如果喜欢的请转发和收藏!!

你可能感兴趣的:(编写一个弹出式菜单的shell程序并实现_使用Shell函数实现多级菜单系统安装)