| ... | ... |
@@ -88,17 +88,22 @@ function iniget_multiline {
|
| 88 | 88 |
} |
| 89 | 89 |
|
| 90 | 90 |
# Determinate is the given option present in the INI file |
| 91 |
-# ini_has_option config-file section option |
|
| 91 |
+# ini_has_option [-sudo] config-file section option |
|
| 92 | 92 |
function ini_has_option {
|
| 93 | 93 |
local xtrace |
| 94 | 94 |
xtrace=$(set +o | grep xtrace) |
| 95 | 95 |
set +o xtrace |
| 96 |
+ local sudo="" |
|
| 97 |
+ if [ $1 == "-sudo" ]; then |
|
| 98 |
+ sudo="sudo " |
|
| 99 |
+ shift |
|
| 100 |
+ fi |
|
| 96 | 101 |
local file=$1 |
| 97 | 102 |
local section=$2 |
| 98 | 103 |
local option=$3 |
| 99 | 104 |
local line |
| 100 | 105 |
|
| 101 |
- line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
|
|
| 106 |
+ line=$($sudo sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" "$file")
|
|
| 102 | 107 |
$xtrace |
| 103 | 108 |
[ -n "$line" ] |
| 104 | 109 |
} |
| ... | ... |
@@ -173,8 +178,10 @@ function iniset {
|
| 173 | 173 |
xtrace=$(set +o | grep xtrace) |
| 174 | 174 |
set +o xtrace |
| 175 | 175 |
local sudo="" |
| 176 |
+ local sudo_option="" |
|
| 176 | 177 |
if [ $1 == "-sudo" ]; then |
| 177 | 178 |
sudo="sudo " |
| 179 |
+ sudo_option="-sudo " |
|
| 178 | 180 |
shift |
| 179 | 181 |
fi |
| 180 | 182 |
local file=$1 |
| ... | ... |
@@ -187,11 +194,11 @@ function iniset {
|
| 187 | 187 |
return |
| 188 | 188 |
fi |
| 189 | 189 |
|
| 190 |
- if ! grep -q "^\[$section\]" "$file" 2>/dev/null; then |
|
| 190 |
+ if ! $sudo grep -q "^\[$section\]" "$file" 2>/dev/null; then |
|
| 191 | 191 |
# Add section at the end |
| 192 | 192 |
echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null |
| 193 | 193 |
fi |
| 194 |
- if ! ini_has_option "$file" "$section" "$option"; then |
|
| 194 |
+ if ! ini_has_option $sudo_option "$file" "$section" "$option"; then |
|
| 195 | 195 |
# Add it |
| 196 | 196 |
$sudo sed -i -e "/^\[$section\]/ a\\ |
| 197 | 197 |
$option = $value |
| ... | ... |
@@ -228,7 +235,7 @@ function iniset_multiline {
|
| 228 | 228 |
# the reverse order. Do a reverse here to keep the original order. |
| 229 | 229 |
values="$v ${values}"
|
| 230 | 230 |
done |
| 231 |
- if ! grep -q "^\[$section\]" "$file"; then |
|
| 231 |
+ if ! $sudo grep -q "^\[$section\]" "$file"; then |
|
| 232 | 232 |
# Add section at the end |
| 233 | 233 |
echo -e "\n[$section]" | $sudo tee --append "$file" > /dev/null |
| 234 | 234 |
else |
| ... | ... |
@@ -125,14 +125,14 @@ VAL=$(iniget ${TEST_INI} bbb handlers)
|
| 125 | 125 |
assert_equal "$VAL" "33,44" "inset at EOF" |
| 126 | 126 |
|
| 127 | 127 |
# test empty option |
| 128 |
-if ini_has_option ${TEST_INI} ddd empty; then
|
|
| 128 |
+if ini_has_option ${SUDO_ARG} ${TEST_INI} ddd empty; then
|
|
| 129 | 129 |
passed "ini_has_option: ddd.empty present" |
| 130 | 130 |
else |
| 131 | 131 |
failed "ini_has_option failed: ddd.empty not found" |
| 132 | 132 |
fi |
| 133 | 133 |
|
| 134 | 134 |
# test non-empty option |
| 135 |
-if ini_has_option ${TEST_INI} bbb handlers; then
|
|
| 135 |
+if ini_has_option ${SUDO_ARG} ${TEST_INI} bbb handlers; then
|
|
| 136 | 136 |
passed "ini_has_option: bbb.handlers present" |
| 137 | 137 |
else |
| 138 | 138 |
failed "ini_has_option failed: bbb.handlers not found" |