BashConfigurationFiles

20th October 2016 at 4:06pm
Bash CodeSnippets
read_config () {
  declare -x config_file="$1"
  (
    source "$config_file" >/dev/null 2>&1
    for var in $(egrep -io '^[a-z0-9_]+=' "$config_file") ; do
      var="${var%=}"
      echo "$var=${!var:-}" | base64 -w 0
      echo
    done
  ) || :
}

main () {
  declare -x config_file="${1:-}"
  if [[ -r "$config_file" ]]  ; then
    while read -r var ; do
        declare "$(base64 -d <<< "$var")" || :
    done < <(read_config "$config_file")
  fi

  echo "a=${a:-((unset variable))}"
  echo "b=${b:-((unset variable))}"
  echo "c=${c:-((unset variable))}"
  echo "d=${d:-((unset variable))}"
  echo "e=${e:-((unset variable))}"
}

main "$@"
# Configuration file
a=1
b=2
c="test \"quoted\""
d="  line1
  line2
  line3  "
e="${b+$a}"