Centos7 JDK 自动安装脚本

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/bin/bash
# shell script to install jdk


# Check if user is root

if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install JDK"
    exit 1
fi

clear

cur_dir=$(pwd)
sys_bit=$(getconf LONG_BIT)
#software version default value
jdk_pre="7u75-b13/"
jdk_version="jdk-7u75-linux"
jdk_url="jdk1.7.0_75"


#Get system bit version
case "$sys_bit" in
64)
sys_bit="x64"
;;
32)
sys_bit="i586"
;;
*)
echo "We don't know your system bit version,We think it's about x86 bit system"
sys_bit="i586"
esac


#which JDK version do you want to install?
echo "========================================="
echo "which JDK version do you want to install?"
echo "Install JDK 1.7,Please input 7 or press Enter"
echo "Install JDK 1.8,Please input 8"
read -p "(Please input 7 or 8):" jdk_version

case "$jdk_version" in
7)
echo "You will install JDK 1.7(7u75_$sys_bit)"
jdk_pre="7u75-b13/"
jdk_version="jdk-7u75-linux"
jdk_url="jdk1.7.0_75"
;;
8)
echo "You will install JDK 1.8(8u31_$sys_bit)"
jdk_pre="8u31-b13/"
jdk_version="jdk-8u31-linux"
jdk_url="jdk1.8.0_31"
;;
*)
echo "INPUT error,You will install JDK 1.7(7u75_$sys_bit)"
jdk_version="jdk-7u75-linux"
esac


# 1. remove openjdk if exists.
for i in $(rpm -qa | grep openjdk | grep -v grep)
do
  echo "Deleting rpm -> "$i
  rpm -e --nodeps $i
done


function downloadJDK(){

if [ -s jdk-$jdk_version-$sys_bit.tar.gz ]; then
  echo "jdk-$jdk_version-$sys_bit.tar.gz [found]"
  else
  echo "Error: jdk-$jdk_version-$sys_bit.tar.gz not found!!!download now......"
  wget --no-check-certificate --no-cookie --header "Cookie: oraclelicense=accept-securebackup-cookie;" http://download.oracle.com/otn-pub/java/jdk/$jdk_version-$sys_bit.tar.gz
fi

#7x32 http://download.oracle.com/otn-pub/java/jdk/7u75-b13/jdk-7u75-linux-i586.rpm
#7x64 http://download.oracle.com/otn-pub/java/jdk/7u75-b13/jdk-7u75-linux-x64.rpm
#7x32 http://download.oracle.com/otn-pub/java/jdk/7u75-b13/jdk-7u75-linux-i586.tar.gz
#7x64 http://download.oracle.com/otn-pub/java/jdk/7u75-b13/jdk-7u75-linux-x64.tar.gz
#8x64 http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-linux-x64.tar.gz
#8x32 http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-linux-i586.tar.gz
#8x64 http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-linux-x64.rpm
#8x32 http://download.oracle.com/otn-pub/java/jdk/8u31-b13/jdk-8u31-linux-i586.rpm

}


function installJDK(){
# 1.unzip and install JDK
  
  mkdir /usr/java
  tar -zxvf $jdk_version-$sys_bit.tar.gz -C /usr/java
  


  # 2. config /etc/profile


  echo "export JAVA_HOME=/usr/java/$jdk_url" >>/etc/profile
echo -e 'export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar'>>/etc/profile
  echo -e 'export PATH=$PATH:$JAVA_HOME/bin'>>/etc/profile
  source /etc/profile  
}





if [[ ! -z $(rpm -qa | grep openjdk | grep -v grep) ]];
then 
  echo "-->Failed to remove the defult JDK."
else 


# 2. download JDK
downloadJDK  2>&1 | tee -a /root/jdk-install.log
# . install JDK
installJDK  2>&1 | tee -a /root/jdk-install.log

fi