From dfcaf7e4d0f4259dac22c5aa6522d95337b55055 Mon Sep 17 00:00:00 2001 From: Joywayer Date: Thu, 6 Aug 2026 16:08:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(vps-xray):=20update=5Fenv=5Ffield=20?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E6=97=B6=E5=A4=84=E7=90=86=E7=BC=BA=E5=B0=BE?= =?UTF-8?q?=E6=8D=A2=E8=A1=8C=E7=9A=84=E6=96=87=E4=BB=B6=EF=BC=8C=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E5=AD=97=E6=AE=B5=E9=BB=8F=E8=BF=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vps-xray/deploy.sh | 4 ++++ vps-xray/tests/test-restart-schedule.sh | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/vps-xray/deploy.sh b/vps-xray/deploy.sh index c8f659d..b759391 100644 --- a/vps-xray/deploy.sh +++ b/vps-xray/deploy.sh @@ -139,6 +139,10 @@ update_env_field() { esc=$(printf '%s' "$value" | sed 's/[|&\\]/\\&/g') sed -i "s|^${key}=.*|${key}=${esc}|" "$file" else + # 文件末尾可能缺换行(用户手工编辑过),直接追加会把新字段黏到上一行 + if [ -n "$(tail -c 1 "$file")" ]; then + printf '\n' >> "$file" + fi printf '%s=%s\n' "$key" "$value" >> "$file" fi return 0 diff --git a/vps-xray/tests/test-restart-schedule.sh b/vps-xray/tests/test-restart-schedule.sh index f8c4b07..4775c37 100644 --- a/vps-xray/tests/test-restart-schedule.sh +++ b/vps-xray/tests/test-restart-schedule.sh @@ -139,6 +139,11 @@ is "字段总行数正确" "$(grep -c '^XRAY_RESTART_TIMEZONE=' "${TMP}/.env")" update_env_field "${TMP}/nonexistent" FOO bar 2>/dev/null is "文件不存在返回 1" "$?" "1" +printf 'KEY=old' > "${TMP}/.env2" +update_env_field "${TMP}/.env2" NEWKEY val +is "缺尾换行时不黏连" "$(grep -c '^NEWKEY=val$' "${TMP}/.env2")" "1" +is "缺尾换行时原字段完好" "$(grep -c '^KEY=old$' "${TMP}/.env2")" "1" + echo "" echo "通过 ${PASS},失败 ${FAIL}" [ "$FAIL" -eq 0 ]