代码如下:
#!/bin/bash
INSTALL是放安装包的路径
INSTALL=/opt/install/
JDK安装包的文件名
JDK=jdk-8u111-linux-x64.tar.gz
TOMCAT安装包的文件名
TOMCAT=apache-tomcat-8.5.57.tar.gz
BIGDATA是安装的目标路径
BIGDATA=/opt/bigdata/
CMD用于执行命令
CMD=''
show用来展示命令执行的结果
show(){
msg=''
for p in $*
do
msg=$msg' '$p
done
}
err用来判断命令是否执行成功
err(){
show $*
if [ $? -ne 0 ]
then
echo '异常:'$msg
exit 1
else
echo '成功:'$msg
fi
}
此处用来判断目标安装路径是否存在,如果不存在就创建
if [ -e $BIGDATA ]
then
echo "${BIGDATA} exist"
else
CMD="mkdir ${BIGDATA}"
$CMD
err "创建目录 $BIGDATA"
fi
进入放安装包的路径,执行tar解压JDK命令
CMD="cd ${INSTALL}"
$CMD
err "${CMD}"
CMD="tar -zxf ${JDK} -C ${BIGDATA}"
$CMD
err "${CMD}"
执行tar解压TOMCAT命令
CMD="tar -zxf ${TOMCAT} -C ${BIGDATA}"
$CMD
err "${CMD}"
重命名TOMCAT解压后的文件夹名为tomcat85
CMD="mv ${BIGDATA}apache-tomcat-8.5.57/ ${BIGDATA}tomcat85/"
$CMD
err "${CMD}"
重命名JDK解压后的文件夹名为jdk180
CMD="mv ${BIGDATA}jdk1.8.0_111/ ${BIGDATA}jdk180/"
$CMD
err "${CMD}"
添加环境变量至/etc/profile,并使之生效
CMD=`sed -i "/unset i/i\export JAVAHOME=${BIGDATA}jdk180" /etc/profile`
`$CMD`
err "创建环境变量JAVAHOME"
CMD=`sed -i "/unset i/i\export TOMCATHOME=${BIGDATA}tomcat85" /etc/profile`
$CMD
err "创建环境变量TOMCATHOME"
CMD=`sed -i '/unset i/i\export PATH=$JAVAHOME/bin:$TOMCATHOME/bin:$PATH' /etc/profile`
$CMD
err "创建环境变量PATH"
CMD=`sed -i '/unset i/i\export CLASSPATH=.:$JAVAHOME/lib/dt.jar:$TOMCATHOME/lib/tools.jar' /etc/profile`
$CMD
err "创建环境变量CLASSPATH"
source /etc/profile
echo'发布jdk&tomcat成功'