Created
May 25, 2022 12:33
-
-
Save nightsparc/160981a7f2c7e2290b5d58f1416c0592 to your computer and use it in GitHub Desktop.
Read key value pairs from a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @brief Helper to read key value pairs | |
# @details | |
# Read kv in the form <key>=<value> | |
# The script cleans whitespaces first and will remove any newlines | |
# @see https://stackoverflow.com/a/27918723/1267320 | |
read_kvp() | |
{ | |
file="$1" | |
while IFS="=" read -r key value; do | |
# clean whitespaces | |
keyClean=$(echo $key) | |
valClean=$(echo $value) | |
# echo "$keyClean = $valClean" | |
case "$keyClean" in | |
'#'*) ;; | |
*) | |
eval "$keyClean=\"${valClean//[$'\t\r\n']}\"" | |
esac | |
done < "$file" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment