Merge pull request #354 from YunoHost/update_ynh_replace_string

[enh] Escape some special character in ynh_replace_string
This commit is contained in:
Laurent Peuch 2017-10-06 15:03:40 +02:00 committed by GitHub
commit ff4b08ed65

View file

@ -22,8 +22,19 @@ ynh_string_random() {
# (see sed manual page for more information)
ynh_replace_string () {
delimit=@
match_string=${1//${delimit}/"\\${delimit}"} # Escape the delimiter if it's in the string.
# 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//&/"\&"}
# 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"