脚本地址
https://dlk2qiw7lh.feishu.cn/docx/CxphdYsBrocrZmxSV0kca7dBnwg?from=from_copylink
脚本内容:
#!/bin/bash
# 设置 erase 字符以正确处理退格键
stty erase ^H 2>/dev/null || stty erase ^?
# 定义颜色和样式
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
# 判断是否为 root 用户
is_root() {
if [[ "$EUID" -ne 0 ]]; then
return 1
else
return 0
fi
}
# 根据用户权限设置安装目录和环境变量文件
if is_root; then
INSTALL_DIR="/usr/local"
JAVA_DIR="$INSTALL_DIR/java"
ENV_FILE="/etc/profile"
else
USER_INSTALL_DIR="$HOME/java"
JAVA_DIR="$USER_INSTALL_DIR/java"
ENV_FILE="$HOME/.bashrc"
fi
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
# 添加函数以检测系统架构
get_system_arch() {
arch=$(uname -m)
case "$arch" in
x86_64 | amd64 | x64)
echo "x86_64"
;;
armv7l | armv8l | aarch64)
echo "arm64"
;;
*)
echo "unsupported"
;;
esac
}
# 检查 JDK 是否已安装
check_jdk_installed() {
if [[ -d "$JAVA_DIR" || -L "$JAVA_DIR" ]]; then
return 0
else
return 1
fi
}
# 获取已安装 JDK 的版本
get_jdk_version() {
if [[ -x "$JAVA_DIR/bin/java" ]]; then
"$JAVA_DIR/bin/java" -version 2>&1 | awk -F'"' '/version/ {print $2}'
else
echo "Unknown"
fi
}
# 安装 JDK
install_jdk() {
local jdk_archive="$1"
echo -e "${GREEN}即将安装的 JDK 包是:$jdk_archive${NORMAL}" >&2
if [[ ! -f "$jdk_archive" ]]; then
echo -e "${RED}错误:JDK 压缩包 [$jdk_archive] 不存在。${NORMAL}" >&2
return 1
fi
echo -e "${GREEN}解压 JDK 到标准路径...${NORMAL}" >&2
if is_root; then
tar xf "$jdk_archive" -C "$INSTALL_DIR" || { echo -e "${RED}解压失败。${NORMAL}" >&2; return 1; }
# 更可靠地提取目录名
JDK_EXTRACTED_DIR=$(tar -tf "$jdk_archive" | grep '/$' | head -1 | cut -d"/" -f1)
if [[ -z "$JDK_EXTRACTED_DIR" ]]; then
echo -e "${RED}错误:无法提取 JDK 目录名。请检查压缩包结构。${NORMAL}" >&2
return 1
fi
JDK_PATH="$INSTALL_DIR/$JDK_EXTRACTED_DIR"
else
# 对于非 root 用户,安装到用户目录下
mkdir -p "$USER_INSTALL_DIR" || { echo -e "${RED}创建用户安装目录失败。${NORMAL}" >&2; return 1; }
tar xf "$jdk_archive" -C "$USER_INSTALL_DIR" || { echo -e "${RED}解压失败。${NORMAL}" >&2; return 1; }
JDK_EXTRACTED_DIR=$(tar -tf "$jdk_archive" | grep '/$' | head -1 | cut -d"/" -f1)
if [[ -z "$JDK_EXTRACTED_DIR" ]]; then
echo -e "${RED}错误:无法提取 JDK 目录名。请检查压缩包结构。${NORMAL}" >&2
return 1
fi
JDK_PATH="$USER_INSTALL_DIR/$JDK_EXTRACTED_DIR"
fi
if [[ ! -d "$JDK_PATH" ]]; then
echo -e "${RED}错误:解压后的 JDK 目录 [$JDK_PATH] 不存在。${NORMAL}" >&2
return 1
fi
if is_root; then
echo -e "${GREEN}创建软链接到标准路径...${NORMAL}" >&2
sudo rm -rf "$JAVA_DIR"
sudo ln -s "$JDK_PATH" "$JAVA_DIR" || { echo -e "${RED}创建软链接失败。${NORMAL}" >&2; return 1; }
sudo chown -R root:root "$JAVA_DIR"
else
# 对于非 root 用户,设置用户级别的 JAVA_HOME
rm -rf "$JAVA_DIR"
ln -s "$JDK_PATH" "$JAVA_DIR" || { echo -e "${RED}创建软链接失败。${NORMAL}" >&2; return 1; }
fi
if ! grep -q "JAVA_HOME=" "$ENV_FILE"; then
echo -e "${GREEN}设置环境变量...${NORMAL}" >&2
if is_root; then
sudo tee -a "$ENV_FILE" >/dev/null <<'EOF'
export JAVA_HOME=/usr/local/java
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
EOF
else
cat >> "$ENV_FILE" <<EOF
export JAVA_HOME=$JAVA_DIR
export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH
export CLASSPATH=.:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib:\$JAVA_HOME/lib/tools.jar
EOF
fi
fi
# 重新载入环境变量
if is_root; then
source "$ENV_FILE"
else
source "$ENV_FILE"
fi
echo -e "${GREEN}设置环境变量完成。${NORMAL}" >&2
if is_root; then
echo -e "${GREEN}请执行 'source /etc/profile' 或重新登录以使环境变量生效。${NORMAL}" >&2
else
echo -e "${GREEN}请执行 'source ~/.bashrc' 或重新登录以使环境变量生效。${NORMAL}" >&2
fi
echo -e "${GREEN}JDK 安装完成,版本信息如下:${NORMAL}" >&2
"$JDK_PATH/bin/java" -version >&2
return 0
}
# 自动安装 JDK
auto_install_jdk() {
local arch=$(get_system_arch)
echo -e "${YELLOW}检测到的系统架构: ${BOLD}${arch}${NORMAL}" >&2
if [[ "$arch" == "unsupported" ]]; then
echo -e "${RED}当前系统架构不受支持。仅支持 x86_64 和 arm64 架构。${NORMAL}" >&2
return 1
fi
# 根据架构递归查找 JDK 安装包
if [[ "$arch" == "x86_64" ]]; then
# 支持多种命名方式
mapfile -t jdk_archives < <(find "$SCRIPT_DIR" -type f \( -name "jdk-*-linux-x86_64*.tar.gz" -o -name "jdk-*-linux-x64*.tar.gz" -o -name "jdk-*-linux-amd64*.tar.gz" \))
elif [[ "$arch" == "arm64" ]]; then
# 调整匹配模式以包含更多后缀信息
mapfile -t jdk_archives < <(find "$SCRIPT_DIR" -type f -name "jdk-*-linux-arm64*.tar.gz")
fi
if [[ ${#jdk_archives[@]} -eq 0 ]]; then
echo -e "${RED}当前目录及子目录未找到匹配架构 [$arch] 的 jdk-*.tar.gz 文件,请确认后重试。${NORMAL}" >&2
return 1
fi
if [[ "$1" == "install" ]]; then
# 自动安装第一个包
selected_file="${jdk_archives[0]}"
echo -e "${GREEN}自动选择安装第一个包:$(basename "$selected_file")${NORMAL}" >&2
install_jdk "$selected_file"
return 0
fi
# 交互式选择安装包
echo -e "${YELLOW}找到与当前系统架构匹配的 JDK 安装包,请选择需要安装的版本:${NORMAL}" >&2
count=0
candidates=()
for f in "${jdk_archives[@]}"; do
[[ -f "$f" ]] || continue
count=$((count+1))
candidates+=("$f")
fname=$(basename "$f")
echo -e "${YELLOW}$count. $fname${NORMAL}" >&2
done
# 添加返回主菜单和退出选项
echo -e "${YELLOW}b. 返回主菜单${NORMAL}" >&2
echo -e "${YELLOW}q. 退出脚本${NORMAL}" >&2
while true; do
echo "" >&2
printf "${BLUE}请输入序号、文件名,或选择返回/退出: ${NORMAL}" >&2
read -r choice < /dev/tty
if [[ -z "$choice" ]]; then
echo -e "${RED}输入不能为空,请重新输入。${NORMAL}" >&2
continue
fi
# 处理返回和退出选项
if [[ "$choice" == "b" || "$choice" == "B" ]]; then
echo -e "${GREEN}返回主菜单。${NORMAL}" >&2
return 1 # 返回到主菜单
elif [[ "$choice" == "q" || "$choice" == "Q" ]]; then
echo -e "${GREEN}退出脚本。${NORMAL}" >&2
exit 0
fi
if [[ $choice =~ ^[0-9]+$ ]]; then
if (( choice > 0 && choice <= count )); then
selected_index=$((choice-1))
selected_file="${candidates[$selected_index]}"
echo -e "${GREEN}您选择的安装包是:$(basename "$selected_file")${NORMAL}" >&2
install_jdk "$selected_file"
echo -e "${GREEN}安装完成,退出脚本。${NORMAL}" >&2
exit 0 # 安装完成后退出
else
echo -e "${RED}无效序号,请重新输入。${NORMAL}" >&2
fi
else
# 检查输入是否为文件名
matched=false
for f in "${candidates[@]}"; do
if [[ "$(basename "$f")" == "$choice" ]]; then
selected_file="$f"
matched=true
echo -e "${GREEN}您选择的安装包是:$(basename "$selected_file")${NORMAL}" >&2
install_jdk "$selected_file"
echo -e "${GREEN}安装完成,退出脚本。${NORMAL}" >&2
exit 0 # 安装完成后退出
fi
done
if $matched; then
echo -e "${GREEN}安装完成,退出脚本。${NORMAL}" >&2
exit 0
else
echo -e "${RED}无效输入,请输入序号或文件名重试。${NORMAL}" >&2
fi
fi
done
}
# 卸载 JDK
uninstall_jdk() {
if [[ -L $JAVA_DIR || -d $JAVA_DIR ]]; then
echo "" >&2
printf "${BLUE}是否确认卸载 JDK?(y/n): ${NORMAL}" >&2
read -r confirm < /dev/tty
if [[ $confirm != "y" && $confirm != "Y" ]]; then
echo -e "${GREEN}取消卸载操作。${NORMAL}" >&2
return 0
fi
echo -e "${GREEN}卸载 JDK...${NORMAL}" >&2
if is_root; then
sudo rm -rf "$JAVA_DIR" || { echo -e "${RED}卸载失败。${NORMAL}" >&2; return 1; }
else
rm -rf "$JAVA_DIR" || { echo -e "${RED}卸载失败。${NORMAL}" >&2; return 1; }
fi
echo -e "${GREEN}清除环境变量...${NORMAL}" >&2
if is_root; then
sudo sed -i '/JAVA_HOME/d' "$ENV_FILE"
sudo sed -i '/CLASSPATH/d' "$ENV_FILE"
sudo sed -i '/PATH.*JAVA_HOME/d' "$ENV_FILE"
else
sed -i '/JAVA_HOME/d' "$ENV_FILE"
sed -i '/CLASSPATH/d' "$ENV_FILE"
sed -i '/PATH.*JAVA_HOME/d' "$ENV_FILE"
fi
echo -e "${GREEN}卸载完成,请重新登录或 source 对应的环境变量文件生效。${NORMAL}" >&2
else
echo -e "${YELLOW}未找到 JDK 安装路径,无需卸载。${NORMAL}" >&2
fi
}
# 主菜单
normal_menu() {
while true; do # 添加循环以保持主菜单持续显示
# 动态显示卸载JDK提示
if check_jdk_installed; then
installed_version=$(get_jdk_version)
uninstall_hint="卸载 JDK (当前已安装 JDK: $installed_version)"
else
uninstall_hint="卸载 JDK (当前未安装 JDK)"
fi
echo -e "${BOLD}请选择操作:${NORMAL}" >&2
echo -e "1. 安装 JDK" >&2
echo -e "2. $uninstall_hint" >&2
echo -e "3. 退出脚本" >&2
echo "" >&2
echo -e "${YELLOW}提示:使用 'bash java-install.sh install' 可自动安装第一个匹配的 JDK 包${NORMAL}" >&2
printf "${BLUE}请输入操作序号: ${NORMAL}" >&2
read -r option < /dev/tty
case "$option" in
1)
echo -e "${GREEN}检测 JDK 是否已安装...${NORMAL}" >&2
if check_jdk_installed; then
installed_version=$(get_jdk_version)
echo -e "${YELLOW}JDK 已安装,无需重新安装。(已安装版本:$installed_version)${NORMAL}" >&2
else
echo -e "${GREEN}开始安装 JDK...${NORMAL}" >&2
auto_install_jdk
# 根据 auto_install_jdk 的返回状态决定是否退出
if [[ $? -eq 0 ]]; then
# 安装成功,退出脚本
exit 0
fi
# 如果返回非0,继续主菜单
fi
;;
2)
uninstall_jdk
;;
3)
echo -e "${GREEN}退出脚本。${NORMAL}" >&2
exit 0
;;
*)
echo -e "${RED}无效选项,请重新选择。${NORMAL}" >&2
;;
esac
echo "" >&2 # 添加空行以提高可读性
done
}
# 主逻辑
if [[ "$1" == "install" ]]; then
echo -e "${GREEN}检测 JDK 是否已安装...${NORMAL}" >&2
if check_jdk_installed; then
installed_version=$(get_jdk_version)
echo -e "${YELLOW}JDK 已安装,无需重新安装。(已安装版本:$installed_version)${NORMAL}" >&2
else
echo -e "${GREEN}开始安装 JDK...${NORMAL}" >&2
auto_install_jdk "install"
fi
exit 0
else
normal_menu
fi
标签:jdk,JDK,NORMAL,部署,echo,fi,ChatGPT,DIR
From: https://www.cnblogs.com/superlinux/p/18606992