Update Nlbench.sh

This commit is contained in:
Jensfrank 2024-08-19 15:48:51 +08:00 committed by GitHub
parent c3686314ee
commit b11b633b23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -100,78 +100,77 @@ check_root() {
fi fi
} }
# 检测操作系统类型和版本 # 检测操作系统
detect_os() { detect_os() {
if [ -f /etc/os-release ]; then if [ -f /etc/os-release ]; then
. /etc/os-release . /etc/os-release
os_type=$ID os_type=$ID
os_version=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then elif type lsb_release >/dev/null 2>&1; then
os_type=$(lsb_release -si) os_type=$(lsb_release -si)
os_version=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then elif [ -f /etc/lsb-release ]; then
. /etc/lsb-release . /etc/lsb-release
os_type=$DISTRIB_ID os_type=$DISTRIB_ID
os_version=$DISTRIB_RELEASE elif [ -f /etc/debian_version ]; then
elif [ -f /etc/redhat-release ]; then os_type="debian"
os_type=$(cat /etc/redhat-release | cut -d ' ' -f 1) elif [ -f /etc/fedora-release ]; then
os_version=$(cat /etc/redhat-release | sed 's/.*release \([0-9\.]*\).*/\1/') os_type="fedora"
elif [ -f /etc/gentoo-release ]; then elif [ -f /etc/centos-release ]; then
os_type="gentoo" os_type="centos"
os_version=$(cat /etc/gentoo-release | cut -d ' ' -f 5)
else else
os_type=$(uname -s) os_type=$(uname -s)
os_version=$(uname -r)
fi fi
os_type=$(echo "$os_type" | tr '[:upper:]' '[:lower:]') os_type=$(echo $os_type | tr '[:upper:]' '[:lower:]')
echo -e "${YELLOW}检测到的系统: $os_type $os_version${NC}" echo "检测到的操作系统: $os_type"
} }
# 更新系统 # 更新系统
update_system() { update_system() {
detect_os || return 1 detect_os
if [ $? -ne 0 ]; then
echo -e "${RED}无法检测操作系统。${NC}"
return 1
fi
case "${os_type,,}" in case "${os_type,,}" in
ubuntu|debian|linuxmint|elementary|pop) ubuntu|debian|linuxmint|elementary|pop)
update_cmd="apt-get update" update_cmd="apt-get update"
upgrade_cmd="apt-get upgrade -y" upgrade_cmd="apt-get upgrade -y"
install_cmd="apt-get install -y" clean_cmd="apt-get autoremove -y"
;; ;;
centos|rhel|fedora|rocky|almalinux|openeuler) centos|rhel|fedora|rocky|almalinux|openeuler)
if command -v dnf &>/dev/null; then if command -v dnf &>/dev/null; then
update_cmd="dnf check-update" update_cmd="dnf check-update"
upgrade_cmd="dnf upgrade -y" upgrade_cmd="dnf upgrade -y"
install_cmd="dnf install -y" clean_cmd="dnf autoremove -y"
else else
update_cmd="yum check-update" update_cmd="yum check-update"
upgrade_cmd="yum upgrade -y" upgrade_cmd="yum upgrade -y"
install_cmd="yum install -y" clean_cmd="yum autoremove -y"
fi fi
;; ;;
opensuse*|sles) opensuse*|sles)
update_cmd="zypper refresh" update_cmd="zypper refresh"
upgrade_cmd="zypper update -y" upgrade_cmd="zypper dup -y"
install_cmd="zypper install -y" clean_cmd="zypper clean -a"
;; ;;
arch|manjaro) arch|manjaro)
update_cmd="pacman -Sy" update_cmd="pacman -Sy"
upgrade_cmd="pacman -Su --noconfirm" upgrade_cmd="pacman -Syu --noconfirm"
install_cmd="pacman -S --noconfirm" clean_cmd="pacman -Sc --noconfirm"
;; ;;
alpine) alpine)
update_cmd="apk update" update_cmd="apk update"
upgrade_cmd="apk upgrade" upgrade_cmd="apk upgrade"
install_cmd="apk add" clean_cmd="apk cache clean"
;; ;;
gentoo) gentoo)
update_cmd="emerge --sync" update_cmd="emerge --sync"
upgrade_cmd="emerge -uDN @world" upgrade_cmd="emerge -uDN @world"
install_cmd="emerge" clean_cmd="emerge --depclean"
;; ;;
cloudlinux) cloudlinux)
update_cmd="yum check-update" update_cmd="yum check-update"
upgrade_cmd="yum upgrade -y" upgrade_cmd="yum upgrade -y"
install_cmd="yum install -y" clean_cmd="yum clean all"
;; ;;
*) *)
echo -e "${RED}不支持的 Linux 发行版: $os_type${NC}" echo -e "${RED}不支持的 Linux 发行版: $os_type${NC}"
@ -180,37 +179,69 @@ update_system() {
esac esac
echo -e "${YELLOW}正在更新系统...${NC}" echo -e "${YELLOW}正在更新系统...${NC}"
sudo $update_cmd && sudo $upgrade_cmd sudo $update_cmd
if [ $? -eq 0 ]; then
if [ $? -ne 0 ]; then sudo $upgrade_cmd
echo -e "${RED}系统更新失败。${NC}" if [ $? -eq 0 ]; then
return 1
fi
echo -e "${GREEN}系统更新完成。${NC}" echo -e "${GREEN}系统更新完成。${NC}"
echo -e "${YELLOW}正在清理系统...${NC}"
sudo $clean_cmd
echo -e "${GREEN}系统清理完成。${NC}"
# 检查是否需要重启 # 检查是否需要重启
if [ -f /var/run/reboot-required ]; then if [ -f /var/run/reboot-required ]; then
echo -e "${YELLOW}系统更新需要重启才能完成。请在方便时重启系统。${NC}" echo -e "${YELLOW}系统更新需要重启才能完成。请在方便时重启系统。${NC}"
fi fi
return 0 return 0
fi
fi
echo -e "${RED}系统更新失败。${NC}"
return 1
} }
# 更新系统并安装依赖 # 定义支持的操作系统类型
SUPPORTED_OS=("ubuntu" "debian" "linuxmint" "elementary" "pop" "centos" "rhel" "fedora" "rocky" "almalinux" "openeuler" "opensuse" "sles" "arch" "manjaro" "alpine" "gentoo" "cloudlinux")
# 安装依赖
install_dependencies() { install_dependencies() {
echo -e "${YELLOW}正在检查并安装必要的依赖项...${NC}" echo -e "${YELLOW}正在检查并安装必要的依赖项...${NC}"
# 确保 os_type 已定义
if [ -z "$os_type" ]; then
detect_os
fi
# 更新系统 # 更新系统
update_system || echo -e "${RED}系统更新失败。继续安装依赖项。${NC}" update_system || echo -e "${RED}系统更新失败。继续安装依赖项。${NC}"
# 安装依赖 # 安装依赖
local dependencies=("curl" "wget" "iperf3" "bc") local dependencies=("curl" "wget" "iperf3" "bc")
# 检查是否为支持的操作系统
if [[ ! " ${SUPPORTED_OS[@]} " =~ " ${os_type} " ]]; then
echo -e "${RED}不支持的操作系统: $os_type${NC}"
return 1
fi
case "${os_type,,}" in case "${os_type,,}" in
gentoo) gentoo)
install_cmd="emerge"
for dep in "${dependencies[@]}"; do for dep in "${dependencies[@]}"; do
if ! emerge -p $dep &>/dev/null; then if ! emerge -p $dep &>/dev/null; then
echo -e "${YELLOW}正在安装 $dep...${NC}" echo -e "${YELLOW}正在安装 $dep...${NC}"
if ! sudo emerge $dep; then if ! sudo $install_cmd $dep; then
echo -e "${RED}无法安装 $dep。请手动安装此依赖项。${NC}"
fi
else
echo -e "${GREEN}$dep 已安装。${NC}"
fi
done
;;
alpine)
install_cmd="apk add"
for dep in "${dependencies[@]}"; do
if ! command -v "$dep" &> /dev/null; then
echo -e "${YELLOW}正在安装 $dep...${NC}"
if ! sudo $install_cmd "$dep"; then
echo -e "${RED}无法安装 $dep。请手动安装此依赖项。${NC}" echo -e "${RED}无法安装 $dep。请手动安装此依赖项。${NC}"
fi fi
else else