Fix iniset and his friends
* In python the white spaces are part of the section name
* Handle options with empty value
* Support paths with white spaces
Change-Id: I69a584608853cfdb8b7dce1e24d929216ef2fc41
diff --git a/functions b/functions
index 1b7d130..3bf0655 100644
--- a/functions
+++ b/functions
@@ -460,7 +460,7 @@
local file=$1
local section=$2
local option=$3
- sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" $file
+ sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=.*$\)|#\1|" "$file"
}
# Uncomment an option in an INI file
@@ -469,7 +469,7 @@
local file=$1
local section=$2
local option=$3
- sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" $file
+ sed -i -e "/^\[$section\]/,/^\[.*\]/ s|[^ \t]*#[ \t]*\($option[ \t]*=.*$\)|\1|" "$file"
}
@@ -480,10 +480,20 @@
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#*=}
}
+# 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
@@ -492,18 +502,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
+ echo -e "\n[$section]" >>"$file"
fi
- if [[ -z "$(iniget $file $section $option)" ]]; then
+ if ! ini_has_option "$file" "$section" "$option"; then
# Add it
- sed -i -e "/^\[ *$section *\]/ a\\
+ sed -i -e "/^\[$section\]/ a\\
$option = $value
-" $file
+" "$file"
else
# Replace it
- sed -i -e "/^\[ *$section *\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" $file
+ sed -i -e "/^\[$section\]/,/^\[.*\]/ s|^\($option[ \t]*=[ \t]*\).*$|\1$value|" "$file"
fi
}