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

   使用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. TP框架中的多种方法代码(C,G,L,T,I,N,D,M,A,R,B,U,W,S,F,E)

    C方法 function C($name=null, $value=null,$default=null) { static $_config = array(); // 无参数时获取所有 if (e ...

  2. 存储过程系列二:适用函数wm_concat(column)函数实现字段合并

    1.学习wm_concat函数 oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oraclewm_concat(column)函数实现字段合并 shoppi ...

  3. js日期的初始化的格式

    js在初始化日期对象时,如果有传入日期.则格式有兼容性问题: //下面的写法在谷歌下没有问题,在火狐和ie下有问题var time = new Date('2014-11-27 00:00:00'); ...

  4. Java丨时间判断谁前谁后

    直奔主题: String date_str1 = "2016-06-02 23:03:123"; String date_str2 = "2016-06-03 03:03 ...

  5. albus就是要第一个出场(线性基)

    传送门 这个题题目描述真怪异--就不能说人话吗-- 人话:给定长为n的序列A,定义f(s)为集合s内所有元素异或值,求A的所有子集的f值从小到大排列后,q在其中第一次出现的下标对10086取模的值. ...

  6. 洛谷 P1509 找啊找啊找GF(复习二维费用背包)

    传送门 题目背景 "找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手,你是我的好GF.再见." "诶,别再见啊..." 七夕...七夕...七夕这个日子,对于sq ...

  7. CS231n 2016 通关 第三章-SVM 作业分析

    作业内容,完成作业便可熟悉如下内容: cell 1  设置绘图默认参数 # Run some setup code for this notebook. import random import nu ...

  8. Asset Catalog Help (三)---Adding Image Sets

    Adding Image Sets Organize versions of your images in image sets, which you can add to an asset cata ...

  9. eclipse编译Jmeter源码

    1.在apache官网下载源码和安装包   http://jmeter.apache.org/ 2.  解压     解压安装包和源码包,     将安装包apache-jmeter-3.3 里lib ...

  10. Visual Studio 2010下WorldWind编译问题集合

    首先:获取WORLDWIND最新代码——建议不要直接下载源代码包进行编译,一是因为它并不是最新版本的代码,WW的代码最近经常更新:二是缺很多依赖的类库.建议用TortoiseSVN客户端从source ...