| ... | ... |
@@ -459,7 +459,7 @@ function inicomment() {
|
| 459 | 459 |
local file=$1 |
| 460 | 460 |
local section=$2 |
| 461 | 461 |
local option=$3 |
| 462 |
- sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file |
|
| 462 |
+ sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file" |
|
| 463 | 463 |
} |
| 464 | 464 |
|
| 465 | 465 |
# Uncomment an option in an INI file |
| ... | ... |
@@ -468,7 +468,7 @@ function iniuncomment() {
|
| 468 | 468 |
local file=$1 |
| 469 | 469 |
local section=$2 |
| 470 | 470 |
local option=$3 |
| 471 |
- sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file |
|
| 471 |
+ sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file" |
|
| 472 | 472 |
} |
| 473 | 473 |
|
| 474 | 474 |
|
| ... | ... |
@@ -479,10 +479,20 @@ function iniget() {
|
| 479 | 479 |
local section=$2 |
| 480 | 480 |
local option=$3 |
| 481 | 481 |
local line |
| 482 |
- line=$(sed -ne "/^\[ *$section *\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
|
|
| 482 |
+ line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
|
|
| 483 | 483 |
echo ${line#*=}
|
| 484 | 484 |
} |
| 485 | 485 |
|
| 486 |
+# Determinate is the given option present in the INI file |
|
| 487 |
+# ini_has_option config-file section option |
|
| 488 |
+function ini_has_option() {
|
|
| 489 |
+ local file=$1 |
|
| 490 |
+ local section=$2 |
|
| 491 |
+ local option=$3 |
|
| 492 |
+ local line |
|
| 493 |
+ line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
|
|
| 494 |
+ [ -n "$line" ] |
|
| 495 |
+} |
|
| 486 | 496 |
|
| 487 | 497 |
# Set an option in an INI file |
| 488 | 498 |
# iniset config-file section option value |
| ... | ... |
@@ -491,18 +501,18 @@ function iniset() {
|
| 491 | 491 |
local section=$2 |
| 492 | 492 |
local option=$3 |
| 493 | 493 |
local value=$4 |
| 494 |
- if ! grep -q "^\[ *$section *\]" $file; then |
|
| 494 |
+ if ! grep -q "^\[$section\]" "$file"; then |
|
| 495 | 495 |
# Add section at the end |
| 496 |
- echo -e "\n[$section]" >>$file |
|
| 496 |
+ echo -e "\n[$section]" >>"$file" |
|
| 497 | 497 |
fi |
| 498 |
- if [[ -z "$(iniget $file $section $option)" ]]; then |
|
| 498 |
+ if ! ini_has_option "$file" "$section" "$option"; then |
|
| 499 | 499 |
# Add it |
| 500 |
- sed -i -e "/^\[ *$section *\]/ a\\ |
|
| 500 |
+ sed -i -e "/^\[$section\]/ a\\ |
|
| 501 | 501 |
$option = $value |
| 502 |
-" $file |
|
| 502 |
+" "$file" |
|
| 503 | 503 |
else |
| 504 | 504 |
# Replace it |
| 505 |
- sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file |
|
| 505 |
+ sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file" |
|
| 506 | 506 |
fi |
| 507 | 507 |
} |
| 508 | 508 |
|
| ... | ... |
@@ -57,6 +57,9 @@ handlers=ee,ff |
| 57 | 57 |
|
| 58 | 58 |
[ ccc ] |
| 59 | 59 |
spaces = yes |
| 60 |
+ |
|
| 61 |
+[ddd] |
|
| 62 |
+empty = |
|
| 60 | 63 |
EOF |
| 61 | 64 |
|
| 62 | 65 |
# Test with spaces |
| ... | ... |
@@ -79,13 +82,22 @@ fi |
| 79 | 79 |
|
| 80 | 80 |
# Test with spaces in section header |
| 81 | 81 |
|
| 82 |
-VAL=$(iniget test.ini ccc spaces) |
|
| 82 |
+VAL=$(iniget test.ini " ccc " spaces) |
|
| 83 | 83 |
if [[ "$VAL" == "yes" ]]; then |
| 84 | 84 |
echo "OK: $VAL" |
| 85 | 85 |
else |
| 86 | 86 |
echo "iniget failed: $VAL" |
| 87 | 87 |
fi |
| 88 | 88 |
|
| 89 |
+iniset test.ini "b b" opt_ion 42 |
|
| 90 |
+ |
|
| 91 |
+VAL=$(iniget test.ini "b b" opt_ion) |
|
| 92 |
+if [[ "$VAL" == "42" ]]; then |
|
| 93 |
+ echo "OK: $VAL" |
|
| 94 |
+else |
|
| 95 |
+ echo "iniget failed: $VAL" |
|
| 96 |
+fi |
|
| 97 |
+ |
|
| 89 | 98 |
# Test without spaces, end of file |
| 90 | 99 |
|
| 91 | 100 |
VAL=$(iniget test.ini bbb handlers) |
| ... | ... |
@@ -104,6 +116,29 @@ else |
| 104 | 104 |
echo "iniget failed: $VAL" |
| 105 | 105 |
fi |
| 106 | 106 |
|
| 107 |
+# test empty option |
|
| 108 |
+if ini_has_option test.ini ddd empty; then |
|
| 109 |
+ echo "OK: ddd.empty present" |
|
| 110 |
+else |
|
| 111 |
+ echo "ini_has_option failed: ddd.empty not found" |
|
| 112 |
+fi |
|
| 113 |
+ |
|
| 114 |
+# test non-empty option |
|
| 115 |
+if ini_has_option test.ini bbb handlers; then |
|
| 116 |
+ echo "OK: bbb.handlers present" |
|
| 117 |
+else |
|
| 118 |
+ echo "ini_has_option failed: bbb.handlers not found" |
|
| 119 |
+fi |
|
| 120 |
+ |
|
| 121 |
+# test changing empty option |
|
| 122 |
+iniset test.ini ddd empty "42" |
|
| 123 |
+ |
|
| 124 |
+VAL=$(iniget test.ini ddd empty) |
|
| 125 |
+if [[ "$VAL" == "42" ]]; then |
|
| 126 |
+ echo "OK: $VAL" |
|
| 127 |
+else |
|
| 128 |
+ echo "iniget failed: $VAL" |
|
| 129 |
+fi |
|
| 107 | 130 |
|
| 108 | 131 |
# Test section not exist |
| 109 | 132 |
|
| ... | ... |
@@ -132,6 +167,12 @@ else |
| 132 | 132 |
echo "iniget failed: $VAL" |
| 133 | 133 |
fi |
| 134 | 134 |
|
| 135 |
+if ! ini_has_option test.ini aaa debug; then |
|
| 136 |
+ echo "OK aaa.debug not present" |
|
| 137 |
+else |
|
| 138 |
+ echo "ini_has_option failed: aaa.debug" |
|
| 139 |
+fi |
|
| 140 |
+ |
|
| 135 | 141 |
iniset test.ini aaa debug "999" |
| 136 | 142 |
|
| 137 | 143 |
VAL=$(iniget test.ini aaa debug) |