From 7eaff9fd0a7a553e50f8698070a2cb3c7eda8a1a Mon Sep 17 00:00:00 2001 From: chunzhimoe Date: Fri, 10 Apr 2026 17:23:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Alpine=20=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E6=80=A7=20-=20POSIX=20sh=20=E5=A4=96=E5=B1=82?= =?UTF-8?q?=E5=8C=85=E8=A3=85,=20=E8=87=AA=E5=8A=A8=E5=AE=89=E8=A3=85=20ba?= =?UTF-8?q?sh/openrc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- port.sh | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/port.sh b/port.sh index 8fb4163..f75eb25 100644 --- a/port.sh +++ b/port.sh @@ -1,3 +1,19 @@ +#!/bin/sh +# 安装脚本 - POSIX sh 兼容 +# Alpine 下自动安装 bash 和 openrc +if [ -f /etc/alpine-release ]; then + if ! command -v bash > /dev/null 2>&1; then + echo "Alpine 系统检测到,正在安装 bash..." + apk add --no-cache bash + fi + if ! command -v rc-service > /dev/null 2>&1; then + echo "正在安装 OpenRC..." + apk add --no-cache openrc + mkdir -p /run/openrc + touch /run/openrc/softlevel + fi +fi + cat > /root/port-forward.sh << 'EOF' #!/bin/bash @@ -28,13 +44,25 @@ detect_system() { OS="unknown" fi - if command -v systemctl &> /dev/null && systemctl --version &> /dev/null 2>&1; then + if command -v systemctl > /dev/null 2>&1 && systemctl --version > /dev/null 2>&1; then INIT_SYS="systemd" - elif command -v rc-service &> /dev/null; then + elif command -v rc-service > /dev/null 2>&1; then INIT_SYS="openrc" else - echo -e "${RED}错误: 不支持的初始化系统(需要 systemd 或 OpenRC)${NC}" - exit 1 + # Alpine 下尝试自动安装 OpenRC + if [ "$OS" = "alpine" ]; then + echo -e "${YELLOW}OpenRC 未安装,正在安装...${NC}" + apk add --no-cache openrc + mkdir -p /run/openrc + touch /run/openrc/softlevel + if command -v rc-service > /dev/null 2>&1; then + INIT_SYS="openrc" + fi + fi + if [ -z "$INIT_SYS" ]; then + echo -e "${RED}错误: 不支持的初始化系统(需要 systemd 或 OpenRC)${NC}" + exit 1 + fi fi echo -e "${BLUE}系统: ${OS} | 初始化系统: ${INIT_SYS}${NC}" @@ -47,13 +75,13 @@ detect_system() { check_deps() { # Alpine 下确保 bash 已安装 if [ "$OS" = "alpine" ]; then - if ! command -v bash &> /dev/null; then + if ! command -v bash > /dev/null 2>&1; then echo -e "${YELLOW}bash 未安装,正在安装...${NC}" apk add --no-cache bash fi fi - if ! command -v socat &> /dev/null; then + if ! command -v socat > /dev/null 2>&1; then echo -e "${YELLOW}socat 未安装,正在安装...${NC}" if [ "$OS" = "alpine" ]; then apk add --no-cache socat @@ -853,6 +881,16 @@ main_menu() { done } +# 确保使用 bash 执行 +if [ -z "$BASH_VERSION" ]; then + if command -v bash > /dev/null 2>&1; then + exec bash "$0" "$@" + else + echo "错误: 需要 bash 来运行此脚本" + exit 1 + fi +fi + if [[ $EUID -ne 0 ]]; then echo -e "${RED}此脚本必须以 root 权限运行${NC}" exit 1