shell脚本一键安装jdk1.8和tomcat

#!/bin/bash
set -e
 
################定义函数
sed_sh(){
sed -i '1a export JAVA_HOME=/usr/local/jdk1.8.0_141' /usr/local/tomcat/bin/$1.sh
sed -i '2a export TOMCAT_HOME=/usr/local/tomcat' /usr/local/tomcat/bin/$1.sh
sed -i '3a export CATALINA_HOME=/usr/local/tomcat' /usr/local/tomcat/bin/$1.sh
sed -i '4a export CLASS_PATH=$JAVA_HOME/bin/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tool.jar' /usr/local/tomcat/bin/$1.sh
sed -i '5a export PATH=$PATH:/usr/local/jdk1.8.0_141/bin:/usr/local/tomcat/bin' /usr/local/tomcat/bin/$1.sh
}
 
 
 
######################下载jdk包
echo "开始下载jdk包...."
wget -q -O /opt/jdk-8u141-linux-x64.tar.gz --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz"
 
 
#######解压
cd /opt/
tar -zxf jdk-8u141-linux-x64.tar.gz -C /usr/local/
rm -f jdk-8u141-linux-x64.tar.gz
echo "解压完成...."
 
###################################################添加环境变量
cp /etc/profile /etc/profile.bak
echo 'JAVA_HOME=/usr/local/jdk1.8.0_141' >> /etc/profile
echo 'CLASSPATH=$JAVA_HOME/lib/' >> /etc/profile
echo 'PATH=$PATH:$JAVA_HOME/bin' >> /etc/profile
echo 'export PATH JAVA_HOME CLASSPATH' >> /etc/profile
 
#################################################检查安装完成
source /etc/profile
java -version >> /dev/null
if [ $? = 0 ];then
   echo "jdk install is OK!"
else
   echo "jdk install is fail."
   exit 8
fi
 
##########tomcat安装
read -p "input tomcat version: " tom_version
v1=`echo $tom_version|awk -F'.' '{print $1}'`
wget -q https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-$v1/v$tom_version/bin/apache-tomcat-$tom_version.tar.gz
echo "下载完成...."
tar -zxf apache-tomcat-$tom_version.tar.gz -C /usr/local/
rm -f apache-tomcat-$tom_version.tar.gz
mv /usr/local/apache-tomcat-$tom_version/ /usr/local/tomcat/
sed_sh startup
sed_sh shutdown
echo "配置修改完成..."
/usr/local/tomcat/bin/startup.sh
echo "启动tomcat"
sleep 3
curl -s "http://$(ip a|grep "inet "|grep -v 127|awk '{print $2}'|awk -F'/' '{print $1}'):8080" >> /dev/null
if [ $? != 0 ];then
   echo "tomcat 启动失败...."
   exit 9
else
   echo "tomcat 启动成功...."
fi

chmod +x install.sh

./install.sh

会自动下载jdk1.8,会提示输入tomcat版本号,也可以直接在脚本里写死版本号

[root@localhost ~]# ./install.sh
开始下载jdk包….
解压完成….
java version “1.8.0_141”
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)
jdk install is OK!
input tomcat version: 10.0.2
下载完成….
配置修改完成…
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.8.0_141
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
启动tomcat
tomcat 启动成功….
[root@localhost ~]#

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发