-
Notifications
You must be signed in to change notification settings - Fork 6
/
merge-config.sh
executable file
·71 lines (56 loc) · 1.5 KB
/
merge-config.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
67
68
69
70
71
#!/bin/bash
file1=$1
file2=$2
#-------------------------------------------------------------------------------
trim() {
echo "$1"
}
# Determinate is the given option present in the INI file
# ini_has_option config-file section option
function ini_has_option() {
local file=$1
local section=$2
local option=$3
local line
line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
[ -n "$line" ]
}
# Set an option in an INI file
# iniset config-file section option value
function iniset() {
local file=$1
local section=$2
local option=$3
local value=$4
if ! grep -q "^\[$section\]" "$file"; then
# Add section at the end
echo -e "\n[$section]" >>"$file"
fi
if ! ini_has_option "$file" "$section" "$option"; then
# Add it
sed -i -e "/^\[$section\]/ a\\
$option = $value
" "$file"
else
# Replace it
sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
fi
}
#-------------------------------------------------------------------------------
if [ ! -f "$file1.orig" ] ; then
cp "$file1" "$file1.orig"
fi
section=''
while IFS='=' read -r option value
do
[ -z "$option" ] && continue
[[ "$option" =~ ^# ]] && continue
if [[ "$option" =~ \[.*\] ]] ; then
section=${option#*[}
section=${section%]*}
continue
fi
option=$(trim $option)
value=$(trim $value)
iniset "$file1" "$section" "$option" "$value"
done < $file2