Recently, in setting up a Hadoop cluster, many softwares need to be repeatedly installed, such as jdk, mysql, etc. It is acceptable if there are fewer machines, and it is not so good if there are more machines. It takes time and effort. In this scenario, I Thought of using a shell script to automatically install
I pasted the code below.
Note: I use the jdk1.8 installation package jdk-8u144-linux-x64.tar.gz, and the decompressed folder name is jdk1.8.0_144
#!/bin/bash
#设置本地变量
jdk_name="jdk-8u144-linux-x64.tar.gz"
jdk_path="/usr/java/"
dir=`dirname $0`
#设置系统的环境变量
#注意这里的jdk1.8.0_144是jdk-8u144-linux-x64.tar.gz解压完后的目录
JAVA_HOME=${
jdk_path}jdk1.8.0_144
CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:HOME/bin
#若目录存在,则先删除再创建
if test -e $jdk_path; then
rm -rf $jdk_path
fi
#创建多级目录
mkdir -p $jdk_path
cd $dir
#将jdk的安装包复制到/usr/java/目录下并解压,解压完成后删除安装包
cp $jdk_name $jdk_path
cd $jdk_path
tar -zxvf $jdk_name
rm -rf $jdk_name
#用追加输出的方式向/etc/profile文件末尾追加JAVA环境变量的配置
echo "export JAVA_HOME=$JAVA_HOME">>/etc/profile
echo "export CLASSPATH=$CLASSPATH">>/etc/profile
echo "export PATH=$PATH">>/etc/profile
#最关键的一步,让环境变量生效
source /etc/profile
Operation: Finally, it uses the background operation method, plus print logs, which is convenient for troubleshooting. `javascript
nohup sh xxx.sh >>auto_jdk.log &
###### 安装成功后,直接在任何路径下,输入java javac java -version进行验证即可
如有问题可以在下方评论中说明,我再改进