fix: 修复 Alpine 兼容性 - POSIX sh 外层包装, 自动安装 bash/openrc

This commit is contained in:
chunzhimoe 2026-04-10 17:23:48 +08:00
parent e848a32742
commit 7eaff9fd0a

46
port.sh
View File

@ -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,14 +44,26 @@ 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
# 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