From 276058ca6452e3c5872d85da8440f997a31dbe27 Mon Sep 17 00:00:00 2001 From: Ramesh Padmanabhaiah <22363102+codeforester@users.noreply.github.com> Date: Sun, 26 Jul 2026 17:58:40 -0700 Subject: [PATCH] Fix literal backslashes in file-section processing --- lib/bash/file/lib_file.sh | 26 ++++++++++++++++++++++---- lib/bash/file/tests/lib_file.bats | 13 +++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/bash/file/lib_file.sh b/lib/bash/file/lib_file.sh index 031b154..6db24c5 100644 --- a/lib/bash/file/lib_file.sh +++ b/lib/bash/file/lib_file.sh @@ -114,8 +114,12 @@ __file_make_target_temp__() { __file_section_markers_ordered__() { local target_file="$1" beginning_marker="$2" end_marker="$3" - awk -v START_M="$beginning_marker" -v END_M="$end_marker" ' + BASE_BASH_FILE_START_MARKER="$beginning_marker" \ + BASE_BASH_FILE_END_MARKER="$end_marker" \ + awk ' BEGIN { + START_M = ENVIRON["BASE_BASH_FILE_START_MARKER"] + END_M = ENVIRON["BASE_BASH_FILE_END_MARKER"] in_section = 0 invalid = 0 } @@ -243,8 +247,12 @@ file_section_needs_update() { return 2 fi - if ! awk -v START_M="$beginning_marker" -v END_M="$end_marker" ' + if ! BASE_BASH_FILE_START_MARKER="$beginning_marker" \ + BASE_BASH_FILE_END_MARKER="$end_marker" \ + awk ' BEGIN { + START_M = ENVIRON["BASE_BASH_FILE_START_MARKER"] + END_M = ENVIRON["BASE_BASH_FILE_END_MARKER"] in_section = 0 processed = 0 } @@ -444,8 +452,12 @@ update_file_section() { if [[ "$section_exists" == true ]]; then if [[ "$remove_section" == true ]]; then - if awk -v START_M="$beginning_marker" -v END_M="$end_marker" ' + if BASE_BASH_FILE_START_MARKER="$beginning_marker" \ + BASE_BASH_FILE_END_MARKER="$end_marker" \ + awk ' BEGIN { + START_M = ENVIRON["BASE_BASH_FILE_START_MARKER"] + END_M = ENVIRON["BASE_BASH_FILE_END_MARKER"] in_section = 0 processed = 0 } @@ -466,8 +478,14 @@ update_file_section() { return 0 fi else - if awk -v START_M="$beginning_marker" -v END_M="$end_marker" -v NEW_TEXT_FILE="$new_content_file" ' + if BASE_BASH_FILE_START_MARKER="$beginning_marker" \ + BASE_BASH_FILE_END_MARKER="$end_marker" \ + BASE_BASH_FILE_NEW_CONTENT_FILE="$new_content_file" \ + awk ' BEGIN { + START_M = ENVIRON["BASE_BASH_FILE_START_MARKER"] + END_M = ENVIRON["BASE_BASH_FILE_END_MARKER"] + NEW_TEXT_FILE = ENVIRON["BASE_BASH_FILE_NEW_CONTENT_FILE"] processed = 0 # 0 = not yet processed, 1 = processing, 2 = done } $0 == START_M && processed == 0 { diff --git a/lib/bash/file/tests/lib_file.bats b/lib/bash/file/tests/lib_file.bats index 0a8d5f8..85e9edb 100644 --- a/lib/bash/file/tests/lib_file.bats +++ b/lib/bash/file/tests/lib_file.bats @@ -105,6 +105,19 @@ EOF [ "$(cat "$target")" = $'line-one\n-n\nvalue\n-e' ] } +@test "update_file_section preserves literal backslashes in markers and content" { + local target="$TEST_TMPDIR/config.txt" + local beginning='\t' + local ending='\e' + local replacement='replacement\t' + printf '%s\nold\n%s\nafter\n' "$beginning" "$ending" > "$target" + + update_file_section "$target" "$beginning" "$ending" "$replacement" + + printf -v expected '%s\n%s\n%s\nafter' "$beginning" "$replacement" "$ending" + [ "$(cat "$target")" = "$expected" ] +} + @test "update_file_section preserves symlinks while updating their targets" { local target="$TEST_TMPDIR/config.txt" local link="$TEST_TMPDIR/config-link"