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

   使用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. servlet串行拦截器实现例子

    至于串行过滤器有什么作用,我实在不知.我的理解是它只是说明 过滤器的串行运行方式 需求:当用户没有登录访问更新页面的时候,跳转到登录页面 1.登录页面:login.jsp <%@ page la ...

  2. POJ 2970 The lazy programmer(贪心+单调优先队列)

    A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is ...

  3. kali-linux简单学习(二)

    一.SET 社会工程学工具包有一个叫devolution. 启动 setoolkit 里面可以进行一些钓鱼攻击. tabnabbing  attack这种方式是完整克隆一个网站挂到SET创建的web服 ...

  4. 多线程之:volatile变量的一次写操作的过程

    一:对volatile修饰的变量进行一次写操作的完整过程   在 java 垃圾回收整理一文中,描述了jvm运行时刻内存的分配.其中有一个内存区域是jvm虚拟机栈,每一个线程运行时都有一个线程栈,线程 ...

  5. java中的异常The given object has a null identifier

    修改页面点击提交时报如下异常: org.hibernate.TransientObjectException: The given object has a null identifier: com. ...

  6. TX2上yolov3精度和速度优化方向

    速度优化的方向: 1.减少输入图片的尺寸, 但是相应的准确率可能会有所下降2.优化darknet工程源代码(去掉一些不必要的运算量或者优化运算过程)3.剪枝和量化yolov3网络(压缩模型---> ...

  7. node.js版本管理(Win) --- nvm-window

    目录 1. 安装 2. 使用 1. 安装 去往Git链接:https://github.com/coreybutler/nvm-windows. 点击下载链接: 选择第一个nvm-noinstall. ...

  8. Windows Vista for Developers——第四部分:用户帐号控制(User Account Control,UAC)

    作者:Kenny Kerr 翻译:Dflying Chen 原文:http://weblogs.asp.net/kennykerr/archive/2006/09/29/Windows-Vista-f ...

  9. bzoj1598

    K短路 和超级钢琴之类的差不多 先反图跑最短路,从原点向外拓展,每个点最多拓展k次,否则不可能是k短路 #include<bits/stdc++.h> using namespace st ...

  10. system(“pause”)和getchar()

    大家都知道system(“PAUSE”)可以让C程序在运行结束之前暂停运行.用system(“PAUSE”)可以解决运行程序一闪而过,看不到输出结果的问题.有程序员会用system(“PAUSE”)只 ...