fix: 修复智能端口检测在set -e模式下导致脚本退出的问题

问题描述:
- check_port_status函数返回非零值导致脚本在set -e模式下退出
- 使用grep -oP在某些Debian系统上不兼容

修复内容:
- 修改check_port_status始终返回0,避免触发set -e退出
- 将grep -oP替换为sed命令,提高兼容性
- 通过echo返回状态字符串而非返回码

测试场景:
- Debian 12系统验证通过
- 端口检测功能正常工作

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
WanWanYun
2025-11-13 01:12:13 +08:00
parent 20d23509e7
commit 59a693ff15

View File

@@ -694,27 +694,27 @@ check_port_status() {
fi
if [[ -z "$process" ]] && command -v ss &> /dev/null; then
process=$(ss -tulnp 2>/dev/null | grep ":${port} " | grep -oP '\".*?\"' | tr -d '"' | cut -d',' -f1 | head -1)
# 使用sed替代grep -oP以提高兼容性
process=$(ss -tulnp 2>/dev/null | grep ":${port} " | sed -n 's/.*users:(("\([^"]*\)".*/\1/p' | head -1)
fi
# 3. 根据进程返回状态
# 3. 根据进程返回状态始终返回0以避免set -e导致脚本退出
if [[ -z "$process" ]]; then
# 无法获取进程名(可能权限不足)
echo "occupied"
return 1
elif [[ "$process" == "nginx" ]] || [[ "$process" =~ ^nginx: ]]; then
# Nginx占用
echo "nginx"
return 1
elif [[ "$process" == "apache2" ]] || [[ "$process" == "httpd" ]] || [[ "$process" =~ apache ]]; then
# Apache占用
echo "apache"
return 2
else
# 其他进程
echo "other:$process"
return 3
fi
# 始终返回0避免set -e导致脚本退出
return 0
}
# 改进的端口配置函数