-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplace
47 lines (38 loc) · 1.34 KB
/
replace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
# replace string in file (sed pattern)
# @example
# replace "exit 0" with "# exit 0" in /etc/rc.local
# RPI_REPLACE_PATTERN="exit 0/# exit 0"
# RPI_REPLACE_FILE="${RPI_ROOT}/etc/rc.local"
to_array RPI_REPLACE_PATTERN
to_array RPI_REPLACE_FILE
function rpi_replace_prerun() {
[[ -n "${RPI_REPLACE_PATTERN}" ]] || error "RPI_REPLACE_PATTERN"
[[ -n "${RPI_REPLACE_FILE}" ]] || error "RPI_REPLACE_FILE is not set."
}
function rpi_replace_run() {
local file
local pattern
local i
i=0
for file in "${RPI_REPLACE_FILE[@]}" ; do
pattern="${RPI_REPLACE_PATTERN["${i}"]}"
file="${RPI_REPLACE_FILE["${i}"]}"
rpi_replace_string_in_file "${pattern}" "${file}"
i+=1
done
}
function rpi_replace_description() {
echo "replace string in file (sed pattern \"s/.../g\")"
}
function rpi_replace_help_params() {
help_param "RPI_REPLACE_FILE" "file to search & replace pattern in"
help_param "RPI_REPLACE_PATTERN" "sed pattern to match (and replace)"
}
# ---------------------------------------------------------------------
function rpi_replace_string_in_file() {
local pattern="$1"
local file="$2"
{ [[ -n "${pattern}" ]] && [[ -n "${file}" ]]; } || error "missing arguments. replace line"
sudo sed -E "s/${pattern}/g" -i "${file}" || error "replace_string ${pattern} ${file}"
}