From f57463b27ed1d17cc2dcbf6a9bdbaa3d6fe4edb8 Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Tue, 8 Nov 2011 11:19:15 -0500 Subject: functions: fix a number of shortcomings in parse_envfile Embarassing. This function was just plain broken. - read/trim the correct variables - allow comments (only start of line, no midline) - allow quoting via single or double quotes. Signed-off-by: Dave Reisner --- functions | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'functions') diff --git a/functions b/functions index 387f2af..6cec95b 100644 --- a/functions +++ b/functions @@ -73,7 +73,8 @@ unset TZ unset "${localevars[@]}" parse_envfile() { - local file=$1 validkeys=${@:2} ret=0 lineno=0 key= val= + local file=$1 validkeys=("${@:2}") ret=0 lineno=0 key= val= + local -r quotes=$'[\'"]' comments=$'[;#]*' if [[ -z $file ]]; then printf "error: no environment file specified\n" @@ -94,10 +95,14 @@ parse_envfile() { (( ++lineno )) # trim whitespace, avoiding usage of a tempfile - key=$(echo "$1" | { read -r val; echo "$val"; }) - val=$(echo "$1" | { read -r val; echo "$val"; }) + key=$(echo "$key" | { read -r key; echo "$key"; }) - [[ $key ]] || continue + # key must exist and line must not be a comment + [[ -z $key || ${key:0:1} = $comments ]] && continue + + # trim whitespace, strip matching quotes + val=$(echo "$val" | { read -r val; echo "$val"; }) + [[ ${val:0:1} = $quotes && ${val:(-1)} = "${val:0:1}" ]] && val=${val:1:(-1)} if [[ -z $val ]]; then printf "error: found key \`%s' without value on line %s of %s\n" \ @@ -107,8 +112,8 @@ parse_envfile() { fi # ignore invalid keys if we have a list of valid ones - if (( ${#validkeys[*]} )); then - in_array "$key" "${validkeys[@]}" || continue + if (( ${#validkeys[*]} )) && ! in_array "$key" "${validkeys[@]}"; then + continue fi export "$key=$val" || (( ++ret )) -- cgit v1.2.3