-
Notifications
You must be signed in to change notification settings - Fork 14
/
sed_multiple_files.sh
executable file
·66 lines (59 loc) · 1.44 KB
/
sed_multiple_files.sh
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
#
# Bash script to replace chars in multiple files using sed.
# Backslashes are allowed in the string
#
# Author: luismartingil
# Year: 2013
#
# Some variables to be changed.
URL=http://127.0.0.1
PORT=7788
FILE1=/etc/myprogram1.conf
FILE2=/etc/myprogram2.conf
# Be careful while editing this array.
# Items should be 'ori_str' 'dst_str' 'path_file'
array_changes=(
# Example1. 'ori_str' 'dst_str' 'path_file'
'define("URL", "_url_");'
'define("URL", "'$URL'");'
$FILE1
# Example2. 'ori_str' 'dst_str' 'path_file'
'define("PORT", "_port_");'
'define("PORT", "'$PORT'");'
$FILE1
# Example3. 'ori_str' 'dst_str' 'path_file'
';http://domain.net'
';http://domain.net/mypath'
$FILE2
# Example4. 'ori_str' 'dst_str' 'path_file'
';debug=info'
'debug=info'
$FILE2
)
# Don't modify!
# Looping over the array_changes array applying the sed translations.
# Looping technique is a little bit ugly...
i=0
for item in "${array_changes[@]}"
do
n=$((i%3))
# Source string for the sed command
if [ "$n" -eq "0" ]
then
src=$(echo "$item" | sed 's/\//\\\//g')
# Dest string for the sed command
elif [ "$n" -eq "1" ]
then
dst=$(echo "$item" | sed 's/\//\\\//g')
# TODO. src and dst are obviously
# sharing some code. DRY!
else
# File for the sed command
f=$item
str="s/"$src"/"$dst"/g"
cmd=( sudo sed -i "'"$str"'" $f )
eval "${cmd[@]}"
fi
(( ++i ))
done