fix(vps-xray): update_env_field 追加时处理缺尾换行的文件,防止字段黏连
This commit is contained in:
@@ -139,6 +139,10 @@ update_env_field() {
|
|||||||
esc=$(printf '%s' "$value" | sed 's/[|&\\]/\\&/g')
|
esc=$(printf '%s' "$value" | sed 's/[|&\\]/\\&/g')
|
||||||
sed -i "s|^${key}=.*|${key}=${esc}|" "$file"
|
sed -i "s|^${key}=.*|${key}=${esc}|" "$file"
|
||||||
else
|
else
|
||||||
|
# 文件末尾可能缺换行(用户手工编辑过),直接追加会把新字段黏到上一行
|
||||||
|
if [ -n "$(tail -c 1 "$file")" ]; then
|
||||||
|
printf '\n' >> "$file"
|
||||||
|
fi
|
||||||
printf '%s=%s\n' "$key" "$value" >> "$file"
|
printf '%s=%s\n' "$key" "$value" >> "$file"
|
||||||
fi
|
fi
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@@ -139,6 +139,11 @@ is "字段总行数正确" "$(grep -c '^XRAY_RESTART_TIMEZONE=' "${TMP}/.env")"
|
|||||||
update_env_field "${TMP}/nonexistent" FOO bar 2>/dev/null
|
update_env_field "${TMP}/nonexistent" FOO bar 2>/dev/null
|
||||||
is "文件不存在返回 1" "$?" "1"
|
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 ""
|
||||||
echo "通过 ${PASS},失败 ${FAIL}"
|
echo "通过 ${PASS},失败 ${FAIL}"
|
||||||
[ "$FAIL" -eq 0 ]
|
[ "$FAIL" -eq 0 ]
|
||||||
|
|||||||
Reference in New Issue
Block a user