mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
New helper ynh_replace_string (#280)
* New helper ynh_substitute_char Sed abstraction, and a way to use something other than sed. I added also an escape of the delimiter used by sed, to prevent a fail in this case. * Renaming to ynh_replace_string * Renaming variable for clarity
This commit is contained in:
parent
86f0978dfb
commit
c31e4c5b0d
1 changed files with 15 additions and 0 deletions
|
@ -9,3 +9,18 @@ ynh_string_random() {
|
||||||
| tr -c -d 'A-Za-z0-9' \
|
| tr -c -d 'A-Za-z0-9' \
|
||||||
| sed -n 's/\(.\{'"${1:-24}"'\}\).*/\1/p'
|
| sed -n 's/\(.\{'"${1:-24}"'\}\).*/\1/p'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Substitute/replace a string by another in a file
|
||||||
|
#
|
||||||
|
# usage: ynh_replace_string match_string replace_string target_file
|
||||||
|
# | arg: match_string - String to be searched and replaced in the file
|
||||||
|
# | arg: replace_string - String that will replace matches
|
||||||
|
# | 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.
|
||||||
|
replace_string=${2//${delimit}/"\\${delimit}"}
|
||||||
|
workfile=$3
|
||||||
|
|
||||||
|
sudo sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}g" "$workfile"
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue