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