转自:http://futeng.iteye.com/blog/2064143
需求
- 能自动检测可能默认安装的openJDK,并能删除之。
- 能自动配置JDK在 /etc/profile的环境。
- 可携带参数,参数为普通用户名,可将JDK相关配置进普通用户的 .bash_profile环境。
一键安装脚本
installJDK.sh
#!/bin/bash # shell script to install jdk (default version jdk-6u45-linux-x64.bin) # example : ./installJDK.sh or ./installJDK.sh newLinuxUsername # version 1.0 # created by ifuteng@gmail.com 2014/5/7 # 1. remove openjdk if exists. for i in $(rpm -qa | grep jdk | grep -v grep) do echo "Deleting rpm -> "$i rpm -e --nodeps $i done if [[ ! -z $(rpm -qa | grep jdk | grep -v grep) ]]; then echo "-->Failed to remove the defult Jdk." else # 2.unzip and install JDK(jdk-6u45-linux-x64.bin) chmod u+x ./jdk-6u45-linux-x64.bin ./jdk-6u45-linux-x64.bin mkdir /usr/java mv ./jdk1.6.0_45 /usr/java/jdk1.6.0_45 rm -rf ./jdk1.6.0_45 # 3. config /etc/profile cp /etc/profile /etc/profile.beforeAddJDKenv.20140507.bak echo "JAVA_HOME=/usr/java/jdk1.6.0_45" >> /etc/profile echo "CLASSPATH=.:$JAVA_HOME/lib.tools.jar" >> /etc/profile echo "PATH=$JAVA_HOME/bin:$PATH" >> /etc/profile echo "export JAVA_HOME CLASSPATH PATH" >> /etc/profileo #echo "-->JDK environment has been successed set in /etc/profile." # 4. config user's .bash_profile if [[ -z "$1" ]] ; then #echo "-->Config .bash_profile for JDK environment from $1" username=$1 user_bash_file=/home/$username/.bash_profile #cp $user_bash_file user_bash_file.beforeAddJDKenv.20140507.bak cp /home/$username/.bash_profile /home/$username/.bash_profile.beforeAddJDKenv.20140507.bak echo "export JAVA_HOME=/usr/share/jdk1.6.0_20" >> $user_bash_file echo "export PATH=$JAVA_HOME/bin:$PATH" >> $user_bash_file echo "export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar" >> $user_bash_file fi # 5. Test JDK evironment if [[ ! -z $(ls /user/java/jdk1.6.0_45) ]]; then echo "-->Failed to install JDK (jdk-6u45-linux-x64 : /usr/java/jdk1.6.0_45)" else echo "-->JDK has been successed installed." echo "java -version" java -version echo "javac -version" javac -version echo "ls $JAVA_HOME"$JAVA_HOME ls $JAVA_HOME fi fi
使用脚本
因为只求简单的实现,所以并为花时间考虑潜在的各种异常。
默认使用jdk-6u45-linux-x64.bin(当然替换也异常方便),注意脚本和安装文件需要放在同个目录下。
将脚本粘贴到任意文本工具,选择jdk-6u45-linux-x64.bin,全部替换为你的JDK版本即可。
使用示例
# 不带参数
./installJDK.sh
# 参数为普通用户名
./installJDK.sh newLinuxUsername