CentOS一键安装jdk17

自己写的脚本,存在自己的服务器,不能用的话说一声


一键安装代码

wget 124.223.56.113/install_jdk17.sh && sudo sh install_jdk17.sh

install_jdkjdk17.sh内容

JDKD=/usr/local/
#设定jdk17安装目录

#检查$JDKD/jdk-17_linux-x64_bin.tar.gz文件是否存在
if [ ! -f $JDKD/jdk-17_linux-x64_bin.tar.gz ];then
    echo "----------------------------------------"
    echo "$JDKD/jdk-17_linux-x64_bin.tar.gz 文件不存在"
    echo "开始下载jdk-17"
    echo "----------------------------------------"
    cd $JDKD/
    wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz
    echo "jdk-17_linux-x64_bin.tar.gz 下载完成"
else
    echo "$JDKD/java 压缩包已存在"
    echo "您已安装jdk-17"
fi
    echo "开始解压jdk-17_linux-x64_bin.tar.gz"
    cd $JDKD/
     tar -zxf jdk-17_linux-x64_bin.tar.gz
    #检测$JDKD/java目录是否存在
if [ -d $JDKD/java ]; then
    echo "$JDKD/java 存在"
    #删除java目录
    rm -rf $JDKD/java/*
    rmdir $JDKD/javafi
    cd $JDKD/
    mv jdk-17.0.6 java
            echo "----------------------------------------"
    echo "jdk-17解压完成"
    echo "----------------------------------------"
# 配置java环境echo "----------------------------------------"
echo "开始配置java环境"echo "添加全局变量"
echo "----------------------------------------"
grep "JAVA_HOME=$JDKD/java" /etc/profile
    if [ ! $? -eq 0 ]; then
    sed -i '$a export JAVA_HOME=$JDKD/java' /etc/profile
    sed -i '$a export PATH=$PATH:$JAVA_HOME/bin;' /etc/profile
    sed -i '$a export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar;' /etc/profile
    echo "----------------------------------------"
    echo "java环境配置完成"
    echo "----------------------------------------"
else
    echo "----------------------------------------"
    echo "java环境全局变量已存在"
    echo "----------------------------------------"
fi
#检测配置是否成功
echo "----------------------------------------"
echo "检测配置是否成功"
echo "----------------------------------------"
source /etc/profile
java -version
if [ ! $? -eq 0 ]; then
    rm -rf $JDKD/java/*
    rmdir $JDKD/java
    sed -i '/export JAVA_HOME=\/usr\/local\/java/d' /etc/profile
    sed -i '/export PATH=$PATH:$JAVA_HOME\/bin;/d' /etc/profile
    sed -i '/export CLASSPATH=.:$JAVA_HOME\/lib\/dt.jar:$JAVA_HOME\/lib\/tools.jar;/d' /etc/profile
    echo "============================================"
    echo "java安装失败"
    echo "已删除java环境变量"
    echo "已删除/etc/profile/java"
    echo "请尝试重新安装"
    echo "============================================"
    echo "jdk安装失败可能的原因:"
    echo "已存在的jdk-17压缩包损坏"
    echo "因此建议删除已有压缩包"
else
    echo "恭喜你,安装成功了jdk-17"
fi
    java -version
if [ ! $? -eq 0 ]; then
    read -p "是否删除jdk-17压缩包(输入yes确定): " yes;
if [ "$yes" != "yes" ];then
    echo "压缩包已保留"
    echo "目录:$JDKD/jdk-17_linux-x64_bin.tar.gz"
    exit;
else
    echo "------------"
    echo "已删除jdk-17_linux-x64_bin.tar.gz"
    rm $JDKD/jdk-17_linux-x64_bin.tar.gz
    exit
fi
fi

你可能感兴趣的:(centos,linux)