From bb2424e9b878ec3f573b6224d302073ddcff67db Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Wed, 23 Aug 2017 17:25:16 +0200 Subject: [PATCH 1/2] [enh] Escape some special character in ynh_replace_string --- data/helpers.d/string | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/data/helpers.d/string b/data/helpers.d/string index 772681fb9..3e68b382d 100644 --- a/data/helpers.d/string +++ b/data/helpers.d/string @@ -18,8 +18,18 @@ ynh_string_random() { # | arg: target_file - File in which the string will be replaced. ynh_replace_string () { delimit=@ - match_string=${1//${delimit}/"\\${delimit}"} # Escape the delimiter if it's in the string. + # Escape the delimiter if it's in the string. + match_string=${1//${delimit}/"\\${delimit}"} replace_string=${2//${delimit}/"\\${delimit}"} + + # Escape any backslash to preserve them as simple backslash. + match_string=${match_string//\\/"\\\\"} + replace_string=${replace_string//\\/"\\\\"} + + # Escape the & character, who has a special function in sed. + match_string=${match_string//&/"\&"} + replace_string=${replace_string//&/"\&"} + workfile=$3 sudo sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}g" "$workfile" From e3a4b307f7c307820c83afce9489f8a128ffb966 Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Tue, 3 Oct 2017 22:11:24 +0200 Subject: [PATCH 2/2] Fix double backslash in case of delimiter used --- data/helpers.d/string | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/data/helpers.d/string b/data/helpers.d/string index 3e68b382d..677350e56 100644 --- a/data/helpers.d/string +++ b/data/helpers.d/string @@ -18,9 +18,6 @@ ynh_string_random() { # | arg: target_file - File in which the string will be replaced. ynh_replace_string () { delimit=@ - # Escape the delimiter if it's in the string. - match_string=${1//${delimit}/"\\${delimit}"} - replace_string=${2//${delimit}/"\\${delimit}"} # Escape any backslash to preserve them as simple backslash. match_string=${match_string//\\/"\\\\"} @@ -30,6 +27,10 @@ ynh_replace_string () { match_string=${match_string//&/"\&"} replace_string=${replace_string//&/"\&"} + # Escape the delimiter if it's in the string. + match_string=${1//${delimit}/"\\${delimit}"} + replace_string=${2//${delimit}/"\\${delimit}"} + workfile=$3 sudo sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}g" "$workfile"