Skip to content

Instantly share code, notes, and snippets.

@ggtools
Created January 29, 2012 00:06
Show Gist options
  • Save ggtools/1696356 to your computer and use it in GitHub Desktop.
Save ggtools/1696356 to your computer and use it in GitHub Desktop.
Parse .ini file
# Reads a section of the ini file and put the results in variables prefixed by the supplied prefix.
readIniSection() {
local PREFIX="$1"
local SECTION="${2-main}"
# Neat way to parse the ini section from CONFIG_FILE
# shamelessly taken from http://www.tuxz.net/blog/
eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
-e 's/;.*$//' \
-e 's/[[:space:]]*$//' \
-e 's/^[[:space:]]*//' \
-e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
-e "s/^\(.*\)=/${PREFIX}\1=/" \
< $CONFIG_FILE \
| sed -n -e "/^\[$SECTION\]/,/^\s*\[/{/^[^;].*\=.*/p;}"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment