aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Reisner <dreisner@archlinux.org>2011-11-08 11:19:15 -0500
committerDave Reisner <dreisner@archlinux.org>2011-11-08 11:31:43 -0500
commitf57463b27ed1d17cc2dcbf6a9bdbaa3d6fe4edb8 (patch)
tree5c6ebd7df28d8ee1aec7d6a8dde9f96bd1717eba
parenta90ffd4e4a6c92bc733ac3b50f5c5db1560c3e8f (diff)
downloadinitscripts-f57463b27ed1d17cc2dcbf6a9bdbaa3d6fe4edb8.tar.xz
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 <dreisner@archlinux.org>
-rw-r--r--functions17
1 files changed, 11 insertions, 6 deletions
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 ))