一、说明在现实环境中可能需要批量部署服务器,那么在我们已经部署好一台服务以后如果实现剩下的服务批量安装呢:

   使用shell能否实现功能:

   假设我们要部署lamp或者是lnmp如何实现脚本部署?

  使用以下代码可实现:

  部署方法1:

  

 #!/bin/sh
menu ( ){ cat<<END
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want:
END }
menu
read num
echo "you choice $num"
if [ "$num" -eq ]
then
echo "begin install lamp"
/bin/sh /server/scripts/test///install-lamp.sh
if [ $? -eq ]
then
echo "lamp is been install"
exit
else
echo "lamp install error"
exit
fi
elif [ "$num" -eq ]
then
echo "begin install lamp"
/bin/sh /server/scripts/test///install-lnmp.sh
if [ $? -eq ]
then
echo "lnmp is been install"
exit
else
echo "lnmp install error"
exit
fi elif [ "$num" -eq ]
then
echo "logout"
exit
fi
fi

部署方法1

  测试:

  

  

 [root@localhost script]# sh manu.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want: you choice
begin install lamp
/bin/sh: /server/scripts/test///install-lamp.sh: 没有那个文件或目录
lamp install error
[root@localhost script]# sh manu.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want: you choice
begin install lamp
/bin/sh: /server/scripts/test///install-lnmp.sh: 没有那个文件或目录
lnmp install error
[root@localhost script]# sh manu.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want: you choice
logout #由于真正的安装脚本没有开发所以每次执行安装都会报错没有文件或者目录,生产环境中将安装脚本写进去可实现一键化安装。

测试

    部署方法2:

  

 #!/bin/sh
menu ( ){ cat<<END
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want:
END }
menu
read num
echo "you choice $num" function lamp(){
if [ "$num" -eq ]
then
echo "begin install lamp"
/bin/sh /server/scripts/test///install-lamp.sh
if [ $? -eq ]
then
echo "lamp is been install"
exit else
echo "lamp install error"
exit
fi
fi
} function lnmp(){
if [ "$num" -eq ]
then
echo "begin install lamp"
/bin/sh /server/scripts/test///install-lnmp.sh
if [ $? -eq ]
then
echo "lnmp is been install"
exit
else
echo "lnmp install error"
exit
fi
fi
}
function quit(){
if [ "$num" -eq ]
then
echo "logout..."
exit
fi
}
case $num in
)
lamp
;;
)
lnmp
;;
)
quit
;; *)
echo "USAG:start|stop|restart|status"
esac

部署方法2

  测试:

  

  

 [root@localhost script]# sh  manu2.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want: you choice
begin install lamp
/bin/sh: /server/scripts/test///install-lamp.sh: 没有那个文件或目录
lamp install error
[root@localhost script]# sh manu2.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want: you choice
begin install lamp
/bin/sh: /server/scripts/test///install-lnmp.sh: 没有那个文件或目录
lnmp install error
[root@localhost script]# sh manu2.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want: you choice
logout...
[root@localhost script]# sh manu2.sh
.[install lamp]
.[install lnmp]
.[exit]
pls input the num that you want:
asdf
you choice asdf
USAG:start|stop|restart|status

方法2测试

  经过测试方法2更为优雅,当然其实方法一也能实现方法2中输入错误给个提示。

三、编写一键化安装脚本:

  1、LAMP安装这里以只安装apache为例子:

  

  

 #!/bin/sh
#############################################
# this script is created by xuxuedong. #
# e_mail:@qq.com #
# qqinfo: #
# This install LAMP for auto. #
# version:1.1 #
#############################################
. /etc/init.d/functions
#set env
export PATH=$PATH:/bin:/sbin:/usr/sbin
export LANG="zh_CN.GB18030"
####define CMD and INstall_path
Instal_path=/application/
APP_PACKAGE_PATH=/home/tools
HTTP_RPM=`rpm -qa http*`
mkdir -p ${Instal_path}
if [ `$HTTP_RPM|wc -l` -gt ]
then
for n in $HTTP_RPM;do rpm -e --nodeps $n;done
else
echo "begin install LAMP."
fi
if [ ! -e ${APP_PACKAGE_PATH} ]
then
mkdir ${APP_PACKAGE_PATH}
else
yum install gcc* -y
cd ${APP_PACKAGE_PATH}
wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.2.32.tar.gz
fi if [ $? -eq ]
then
tar -zxf httpd-2.2..tar.gz
if [ $? -eq ]
then
cd httpd-2.2.
./configure --prefix=/application/apache.2.2. --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker --enable-deflate --enable-rewrite
fi
if [ $? -eq ]
then
make
make install
fi
if [ $? -eq ]
then
ln -s /application/apache.2.2. /application/apache
fi
fi

以安装apache为例子

   注明在第二项的一键化安装部署的部署脚本路径上/bin/sh /server/scripts/test///install-lamp.sh 给的是实际的脚本路径。

  实际路径:

  [root@localhost script]# ls | grep lamp
  lamp.sh
  [root@localhost script]# pwd
  /server/script

  所以需要将/bin/sh /server/scripts/test///install-lamp.sh修改成/bin/sh /server/script/lamp.sh

  测试:

  

证明已经开始安装。

  安装结果已经完成了apache的安装,剩下的mysql和php环境安装也可参考脚本:

  

  

  备注:以上脚本可根据实际情况优化,例如:不需要在屏幕中输出,只在有报错是输出报错情况,安装完成输出安装成功即可。此部署脚步前期最好是能确定需要那些关联库,执行安装前最好是所有的安装环境已经准备好了。以上脚本只做为参考,部分需要根据实际情况开发脚本。劲量不要照搬。

  

  

自己开发shell脚本实现一键化安装。的更多相关文章

  1. 编写shell脚本实现一键创建KVM虚拟机

    shell脚本一键创建虚拟机 代码如下: #!/bin/bashname=$1 #把位置变量$1重新定义为name(创建虚拟机的名字)path1=/var/lib/libvirt/images/ #i ...

  2. idea开发shell脚本并运行

    参考:https://blog.csdn.net/u012443641/article/details/81295999 IEDA中的bashsupport插件支持在IDEA中编写shell脚本文件, ...

  3. shell脚本之nginx的安装

           为了编写nginx自动部署的脚本而刚学习的shell脚本语言.写文章只是为了记录,有错误勿喷. 一.创建shell脚本程序        操作系统是Linux的 CentOS 7 版本. ...

  4. 写了shell脚本想一键启动三台虚拟机的Zookeeper,却不知道为啥总是启动不了

    首先,一键启动的shell脚本是这样的 #! /bin/bash case $1 in "start"){ for i in node01 node02 node03 do ssh ...

  5. linux shell脚本使用结构化命令

    内容: 一.if-then命令 二.if-then-else命令 三.test命令 四.case命令 1.if-then结构化命令中最基本的类型,其格式如下: if command then comm ...

  6. 【shell脚本】一键部署LNMP===deploy.sh

    一键部署mysql,php,nginx,通过源码安装部署 #!/bin/bash # 一键部署 LNMP(源码安装版本) menu() { clear echo " ############ ...

  7. shell脚本之一键部署openV~P~N

    提前准备:/root目录下: checkpsw.sh ## 官方提供的自定义脚本,可在http://openvpn.se/files/other/checkpsw.sh下载 openvpn@.serv ...

  8. shell脚本之结构化命令if...then...fi

    if的用法日常主要用于数值或者字符串的比较来实现结构化的,模拟人脑,就是如果遇到什么事情,我们应该做什么 语法格式分为 1. if command;then command;fi    (如果if满足 ...

  9. linux shell脚本使用结构化命令(2)

    一.for命令 二.while命令 三.until命令 1.for命令基本格式 for var in list do commands done oracle@suse:~/testshell> ...

随机推荐

  1. BZOJ_1044_[HAOI2008]木棍分割_二分答案+DP+单调队列

    BZOJ_1044_[HAOI2008]木棍分割_二分答案+DP Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个 ...

  2. [SDOI 2015] 星际战争

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3993 [算法] 首先发现问题具有单调性 , 不妨二分答案mid 考虑网络流 : 将源 ...

  3. 给YUI Compressor添加右键命令,完成快捷压缩

    YUI Compressor默认不带右键安装功能 YUI Compressor非常好用,特别是JS的混淆是众多JS Coding的最爱.可惜官网提供的版本都不具备右键功能,每次压缩都要cmd输入一些命 ...

  4. dcos下rexray服务的配置

    在dcos环境下,rexray服务的默认配置文件为/opt/mesosphere/etc/rexray.conf,而其服务文件则是 /etc/systemd/system/dcos-rexray.se ...

  5. Ubuntu 16.04使用chrome闪屏

    使用Chrome的时候上端经常出现闪动的情况, 但是速度特别快, 根本无法截图, 感觉特别扎心, 以为自己的电脑出现问题了或者显卡驱动出现问题了, 后来才发现问题, 只需要关闭Chrome的硬件加速就 ...

  6. 1.10-1.11 hive交互式命令讲解

    一.hive 交互式命令参数 #帮助 [root@hadoop-senior hive-0.13.1]# bin/hive -h Missing argument for option: h usag ...

  7. python---socket与socketserver

    1.socket的方socket.getaddrinfo(host, port, family=0, type=0, proto=0, flags=0) #获取要连接的对端主机地址sk.bind(ad ...

  8. 在 beforeSend中设置ajax请求的Content-type

    $.ajaxSetup({        beforeSend: function (xhr, settings) {            if (settings.type == "PO ...

  9. Ogre 学习记录

    http://www.cppblog.com/richardhe/articles/55722.html 1: 设计初衷 它设计初衷是完全跨平台的.抽象的接口隐藏了平台相关的细节. 它设计初衷是大幅度 ...

  10. apringboot aop权限控制

    + 定义切面: ···@Aspect @Component public class LoginInterceptor { @Around("@annotation(lock)") ...