From d83c95e413ad6cbffbfe9f2e88c2d1e0d8b03b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=85=8E=E9=A5=BC=E6=9E=9C=E5=AD=90=E5=8D=B7=E9=B2=A8?= =?UTF-8?q?=E9=B1=BC=E8=BE=A3=E6=A4=92?= Date: Tue, 18 Feb 2025 14:53:10 +0800 Subject: [PATCH] feat: Enhance config file modification with special character escaping - Added robust character escaping for keys and values in JSON configuration - Used sed to safely handle special characters during file modification - Improved reliability of modify_or_add_config function - Prevented potential sed parsing errors with advanced escaping techniques --- scripts/run/cursor_linux_id_modifier.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/scripts/run/cursor_linux_id_modifier.sh b/scripts/run/cursor_linux_id_modifier.sh index 3fb5daa..36915c0 100644 --- a/scripts/run/cursor_linux_id_modifier.sh +++ b/scripts/run/cursor_linux_id_modifier.sh @@ -167,6 +167,10 @@ modify_or_add_config() { local value="$2" local file="$3" + # 转义特殊字符 + local key_escaped=$(sed 's/[\/&]/\\&/g' <<< "$key") + local value_escaped=$(sed 's/[\/&]/\\&/g' <<< "$value") + if [ ! -f "$file" ]; then log_error "文件不存在: $file" return 1 @@ -183,15 +187,15 @@ modify_or_add_config() { # 检查key是否存在 if grep -q "\"$key\":" "$file"; then - # key存在,执行替换 - sed "s|\"$key\":[[:space:]]*\"[^\"]*\"|\"$key\": \"$value\"|" "$file" > "$temp_file" || { + # 使用#作为分隔符避免冲突,并转义特殊字符 + sed "s#\"${key_escaped}\":[[:space:]]*\"[^\"]*\"#\"${key_escaped}\": \"${value_escaped}\"#" "$file" > "$temp_file" || { log_error "修改配置失败: $key" rm -f "$temp_file" return 1 } else - # key不存在,添加新的key-value对 - sed "s/}$/,\n \"$key\": \"$value\"\n}/" "$file" > "$temp_file" || { + # 添加新键值对时转义特殊字符 + sed "s/}$/,\n \"${key_escaped}\": \"${value_escaped}\"\n}/" "$file" > "$temp_file" || { log_error "添加配置失败: $key" rm -f "$temp_file" return 1