您当前的位置:首页 > 电脑百科 > 网络技术 > 网络技术

使用Shell函数实现多级菜单系统安装

时间:2020-06-09 20:59:00  来源:  作者:

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

本脚本实现的功能:

使用Shell函数实现多级菜单系统安装

 

演示效果:

1、一级菜单

使用Shell函数实现多级菜单系统安装

 

2、二级菜单

使用Shell函数实现多级菜单系统安装

 


使用Shell函数实现多级菜单系统安装

 

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 "[33[31mFAILED33[0m] $1";}

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

show_info() { echo -e "[33[33mNOTICE33[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 "33[33m*****system time update*****33[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 "33[33m*****system time ntpdate is complete!*****33[0m"

echo ""

sleep 5

}

#install nginx

install_nginx(){

echo ""

echo -e "33[33m*****install nginx*****33[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 '33[33m*****NginX is startd now!*****33[0m'

echo ""

sleep 5

}

# install redis5.0.5

install_redis(){

echo ""

echo -e "33[33m*****install redis*****33[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 '33[33m*****Redis is startd now!*****33[0m'

echo ""

sleep 5

}

# install rabbitmq

install_rabbitmq(){

echo ""

echo -e "33[33m*****install rabbitmq*****33[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 '33[33m*****RabbitMQ is startd now!*****33[0m'

echo ""

sleep 5

}

# install dotnet Sdk

install_dotnet_Sdk(){

echo ""

echo -e "33[33m*****install dotnet sdk*****33[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 '33[33m*****dotnet is startd now!*****33[0m'

echo ""

sleep 5

}

# install dotnet Runtime

install_dotnet_Runtime(){

echo ""

echo -e "33[33m*****install dotnet Runtime*****33[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 '33[33m*****dotnet is startd now!*****33[0m'

echo ""

sleep 5

}

# install mysql 5.7.27

install_mysql(){

echo ""

echo -e "33[33m*****install mysql*****33[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 '33[33m*****MySQL is startd now!*****33[0m'

echo ""

sleep 5

}

#install pgsql 11.5

install_pgsql(){

echo ""

echo -e "33[33m*****install pgsql*****33[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 '33[33m*****PostgreSQL is startd now!*****33[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 "33[33m*****install keepalived master*****33[0m"

/bin/bash ./scripts/keepalived_master.sh

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

echo ""

sleep 5

}

#install keepalived slave

install_keepalived_slave(){

echo ""

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

/bin/bash ./scripts/keepalived_slave.sh

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

echo ""

sleep 5

}

#install redis master

install_redis_master(){

echo ""

echo -e "33[33m*****install redis master*****33[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 '33[33m*****redis master is startd now!*****33[0m'

echo ""

sleep 5

}

#install redis slave

install_redis_salve(){

echo ""

echo -e "33[33m*****install redis slave*****33[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 '33[33m*****redis slave is startd now!*****33[0m'

echo ""

sleep 5

}

# rabbitmq 集群 host

install_rabbitmq_cluster_host(){

echo ""

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

/bin/bash ./scripts/rabbitmq_cluster_host.sh

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

echo ""

sleep 5

}

#install rabbitmq node1

install_rabbitmq_node1(){

echo ""

echo -e "33[33m*****install rabbitmq node1*****33[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 '33[33m*****rabbitmq node1 is startd now!*****33[0m'

echo ""

sleep 5

}

#install rabbitmq node2

install_rabbitmq_node2(){

echo ""

echo -e "33[33m*****install rabbitmq node2*****33[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 '33[33m*****rabbitmq node2 is startd now!*****33[0m'

echo ""

sleep 5

}

#install nginx load

install_nginx_load(){

echo ""

echo -e "33[33m*****install nginx*****33[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 '33[33m*****NginX is startd now!*****33[0m'

echo ""

sleep 5

}

#menu2

menu2(){

while true;

do

clear

cat <<EOF

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

|****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 <<EOF

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

|****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

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



Tags:Shell函数   点击:()  评论:()
声明:本站部分内容及图片来自互联网,转载是出于传递更多信息之目的,内容观点仅代表作者本人,如有任何标注错误或版权侵犯请与我们联系(Email:2595517585@qq.com),我们将及时更正、删除,谢谢。
▌相关推荐
最近因为公司项目需求,项目应用也比较多,所以编写一个shell函数多级菜单自动化部署各种应用的脚本:本脚本实现的功能: 演示效果:1、一级菜单 2、二级菜单 3、脚本参考#!/bin/ba...【详细内容】
2020-06-09  Tags: Shell函数  点击:(60)  评论:(0)  加入收藏
▌简易百科推荐
写一个shell获取本机ip地址、网关地址以及dns信息。经常会遇到取本机ip、网关、dns地址,windows一个命令ipconfig /all全部获取到,但linux系统却并非如此。linux系统都自带ifc...【详细内容】
2021-12-27  K佬食古    Tags:shell   点击:(2)  评论:(0)  加入收藏
步骤1、配置 /etc/sysconfig/network-scripts/ifcfg-eth0 里的文件。it动力的CentOS下的ifcfg-eth0的配置详情:[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifc...【详细内容】
2021-12-24  忆梦如风    Tags:网卡   点击:(10)  评论:(0)  加入收藏
1、查找当前目录下所有以.tar结尾的文件然后移动到指定目录find . -name “*.tar” -execmv {}./backup/ ;注解:find &ndash;name 主要用于查找某个文件名字,-exec 、xargs可...【详细内容】
2021-12-17  郭主任    Tags:运维   点击:(20)  评论:(0)  加入收藏
对于经常上网的朋友来说,除了手机购物上网,pc端玩网页游戏还是很多小伙伴首选的,但是有时候明明宽带链接上了,打开浏览器却出现上不了网的现象,下面小编要来跟大家说说电脑有网络...【详细内容】
2021-12-16  小白系统    Tags:网页无法打开   点击:(28)  评论:(0)  加入收藏
在访问像github、gitlab这样的外国网站时,很有可能会出现页面加载不出来或找不到页面的错误。这时候有的朋友就会以为是网络的问题,于是把Wifi断掉连上自己手机的热点,结果却还...【详细内容】
2021-12-15  启施技术IT狼叔    Tags:外网   点击:(16)  评论:(0)  加入收藏
网络地址来源:获取公网IP地址 https://ipip.yy.com/get_ip_info.phphttp://pv.sohu.com/cityjson?ie=utf-8http://www.ip168.com/json.do?view=myipaddress...【详细内容】
2021-12-15  韦廷华12    Tags:外网ip   点击:(15)  评论:(0)  加入收藏
准备好软件IPOP、用ENSP模拟一下华为交换机 启动交换机 <Huawei>sysEnter system view, return user view with Ctrl+Z.[Huawei]sysname FTPClient[FTPClient]interface vla...【详细内容】
2021-12-15  思源Edward    Tags:交换机   点击:(24)  评论:(0)  加入收藏
我们经常用到netstat命令查看主机连接状况,包括连接ip、端口、状态等,今天就练习下shell分析netsat结果。描述假设netstat命令运行的结果我们存储在nowcoder.txt里,格式如下:Pro...【详细内容】
2021-12-14  K佬食古    Tags:netstat   点击:(19)  评论:(0)  加入收藏
什么是滑动窗口?窗口是操作系统开辟的一块缓存空间,发送方在收到接收方ACK应答之前,必须在缓冲区保留已发送的数据,如果按期收到确认应答,数据就可以从缓冲区移除。什么是滑动窗...【详细内容】
2021-12-14  DifferentJava    Tags:TCP   点击:(30)  评论:(0)  加入收藏
概述日常管理华为路由设备过程中,难为会忘记设备登录密码,那么该如何重置设备登录密码吗?本期文章将全面向各位小伙伴总结分享。重置华为设备登录密码思路先行 采用console登录...【详细内容】
2021-12-10  onme0    Tags:   点击:(27)  评论:(0)  加入收藏
相关文章
    无相关信息
最新更新
栏目热门
栏目头条