- 新建bash文件
点击查看代码
#!/bin/bash
# 1. Check if Java is installed
if type -p java; then
echo "Java has been installed."
exit 0
else
# 2. Install Java
# 检查是否提供了Java安装包的路径
if [ -z "$1" ]; then
echo "Usage: $0 [path_to_jdk_tar.gz]"
exit 1
elif [ ! -f "$1" ]; then
echo "The file does not exist."
exit 1
fi
mkdir -p /opt/java/
# 使用提供的路径解压文件
tar -zxvf "$1" -C /opt/java/
# 3. Configure environment variables
echo "JAVA_HOME=/opt/java/jdk1.8.0_333
PATH=\$JAVA_HOME/bin:\$PATH
CLASSPATH=\$JAVA_HOME/jre/lib/ext:\$JAVA_HOME/lib/tools.jar
export PATH JAVA_HOME CLASSPATH" >> /etc/profile
source /etc/profile
echo "Setting path successful."
java -version
echo "Installation successful."
fi
- 文件授予执行权限
chmod +x 文件名
- 执行启动命令
source ./文件名 jdk压缩包路径
注意:一定要使用 source 启动命令,否则配置文件不会刷新
标签:脚本,java,jdk,centos,echo,Java,HOME,JAVA From: https://www.cnblogs.com/r-ming/p/18131914