Merge "Fix ini functions to handle spaces in section names"
diff --git a/functions b/functions
index 16664d6..b86dbe6 100644
--- a/functions
+++ b/functions
@@ -415,7 +415,7 @@
local file=$1
local section=$2
local option=$3
- sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
+ sed -ie "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
}
# Uncomment an option in an INI file
@@ -424,7 +424,7 @@
local file=$1
local section=$2
local option=$3
- sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
+ sed -ie "/^\[ *$section *\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
}
@@ -435,7 +435,7 @@
local section=$2
local option=$3
local line
- line=$(sed -ne "/^\[$section\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
+ line=$(sed -ne "/^\[ *$section *\]/,/^\[.*\]/ { /^$option[ \t]*=/ p; }" $file)
echo ${line#*=}
}
@@ -447,18 +447,18 @@
local section=$2
local option=$3
local value=$4
- if ! grep -q "^\[$section\]" $file; then
+ if ! grep -q "^\[ *$section *\]" $file; then
# Add section at the end
echo -e "\n[$section]" >>$file
fi
if [[ -z "$(iniget $file $section $option)" ]]; then
# Add it
- sed -i -e "/^\[$section\]/ a\\
+ sed -ie "/^\[ *$section *\]/ a\\
$option = $value
" $file
else
# Replace it
- sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
+ sed -ie "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
fi
}
diff --git a/tests/functions.sh b/tests/functions.sh
index 3a0f319..d2cc5c4 100755
--- a/tests/functions.sh
+++ b/tests/functions.sh
@@ -54,6 +54,9 @@
[bbb]
handlers=ee,ff
+
+[ ccc ]
+spaces = yes
EOF
# Test with spaces
@@ -74,6 +77,14 @@
echo "iniget failed: $VAL"
fi
+# Test with spaces in section header
+
+VAL=$(iniget test.ini ccc spaces)
+if [[ "$VAL" == "yes" ]]; then
+ echo "OK: $VAL"
+else
+ echo "iniget failed: $VAL"
+fi
# Test without spaces, end of file
@@ -112,7 +123,6 @@
echo "iniget failed: $VAL"
fi
-
# Test option not exist
VAL=$(iniget test.ini aaa debug)