1.修改主机名
[root@localhost ~]# vim /etc/hostname
hadoop33
重启虚拟机后,主机名重命名生效
2.创建Shell脚本文件夹
[root@lhadoop33 opt]# mkdir shellfile
3.编写Shell脚本
[root@hadoop33 shellfile]# vim autoinstall.sh
4.Shell脚本命令解析
(1)文件头
#! /bin/bash
(2)打印命令
echo 'auto install begining...'
(3)设置全局变量
# global var
jdk=true
(4)条件判断
if [ "$jdk" = true ];then
fi
(5)解压
tar -zxf /opt/install/jdk-8u321-linux-x64.tar.gz -C /opt/soft
(6)文件重命名
mv /opt/soft/jdk1.8.0_321 /opt/soft/jdk180
(7)在/etc/profile指定行插入
sed -i '73a\export PATH=$PATH:$JAVA_HOME/bin' /etc/profile
sed -i '73a\export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' /etc/profile
sed -i '73a\export JAVA_HOME=/opt/soft/jdk180' /etc/profile
sed -i '73a\# JAVA_HOME' /etc/profile
5.完整脚本内容
#! /bin/bash
echo 'auto install begining...'
# global var
jdk=true
if [ "$jdk" = true ];then
echo 'jkd install set true'
echo 'setup jdk 8'
tar -zxf /opt/install/jdk-8u321-linux-x64.tar.gz -C /opt/soft
mv /opt/soft/jdk1.8.0_321 /opt/soft/jdk180
sed -i '73a\export PATH=$PATH:$JAVA_HOME/bin' /etc/profile
sed -i '73a\export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' /etc/profile
sed -i '73a\export JAVA_HOME=/opt/soft/jdk180' /etc/profile
sed -i '73a\# JAVA_HOME' /etc/profile
echo 'setup jdk8 success!!!'
fi
6.重启环境变量
[root@hadoop33 opt]# source /etc/profile
7.判断java是否配置成功
[root@hadoop33 opt]# javac
[root@hadoop33 opt]# java -version