From 588eb4129d34ea58fd40438eb1c6edd1a9f9a2d0 Mon Sep 17 00:00:00 2001 From: Attila Fazekas Date: Thu, 20 Dec 2012 10:57:16 +0100 Subject: [PATCH] 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 --- functions | 28 +++++++++++++++++++--------- tests/functions.sh | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/functions b/functions index 1b7d1308e7..3bf0655229 100644 --- a/functions +++ b/functions @@ -460,7 +460,7 @@ function inicomment() { 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 @@ function iniuncomment() { 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 @@ function iniget() { 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 @@ function iniset() { 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 } diff --git a/tests/functions.sh b/tests/functions.sh index be48729f71..4fe644367d 100755 --- a/tests/functions.sh +++ b/tests/functions.sh @@ -57,6 +57,9 @@ handlers=ee,ff [ ccc ] spaces = yes + +[ddd] +empty = EOF # Test with spaces @@ -79,13 +82,22 @@ fi # Test with spaces in section header -VAL=$(iniget test.ini ccc spaces) +VAL=$(iniget test.ini " ccc " spaces) if [[ "$VAL" == "yes" ]]; then echo "OK: $VAL" else echo "iniget failed: $VAL" fi +iniset test.ini "b b" opt_ion 42 + +VAL=$(iniget test.ini "b b" opt_ion) +if [[ "$VAL" == "42" ]]; then + echo "OK: $VAL" +else + echo "iniget failed: $VAL" +fi + # Test without spaces, end of file VAL=$(iniget test.ini bbb handlers) @@ -104,6 +116,29 @@ else echo "iniget failed: $VAL" fi +# test empty option +if ini_has_option test.ini ddd empty; then + echo "OK: ddd.empty present" +else + echo "ini_has_option failed: ddd.empty not found" +fi + +# test non-empty option +if ini_has_option test.ini bbb handlers; then + echo "OK: bbb.handlers present" +else + echo "ini_has_option failed: bbb.handlers not found" +fi + +# test changing empty option +iniset test.ini ddd empty "42" + +VAL=$(iniget test.ini ddd empty) +if [[ "$VAL" == "42" ]]; then + echo "OK: $VAL" +else + echo "iniget failed: $VAL" +fi # Test section not exist @@ -132,6 +167,12 @@ else echo "iniget failed: $VAL" fi +if ! ini_has_option test.ini aaa debug; then + echo "OK aaa.debug not present" +else + echo "ini_has_option failed: aaa.debug" +fi + iniset test.ini aaa debug "999" VAL=$(iniget test.ini aaa debug)