Created
July 29, 2014 18:37
-
-
Save dvj/adcd4524d34b4f1d7aed to your computer and use it in GitHub Desktop.
Simple config parser
This file contains hidden or 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
cfg_parser () | |
{ | |
ini="$(<$1)" # read the file | |
ini="${ini//[/\[}" # escape [ | |
ini="${ini//]/\]}" # escape ] | |
IFS=$'\n' && ini=( ${ini} ) # convert to line-array | |
ini=( ${ini[*]//;*/} ) # remove comments with ; | |
ini=( ${ini[*]/\ =/=} ) # remove tabs before = | |
ini=( ${ini[*]/=\ /=} ) # remove tabs be = | |
ini=( ${ini[*]/\ =\ /=} ) # remove anything with a space around = | |
ini=( ${ini[*]/#\\[/\}$'\n'cfg.section.} ) # set section prefix | |
ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1) | |
ini=( ${ini[*]/=/=\( } ) # convert item to array | |
ini=( ${ini[*]/%/ \)} ) # close array parenthesis | |
ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick | |
ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2) | |
ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis | |
ini[0]="" # remove first element | |
ini[${#ini[*]} + 1]='}' # add the last brace | |
eval "$(echo "${ini[*]}")" # eval the result | |
} | |
cfg_parser /path/to/file | |
cfg.section.<SECTION_NAME> | |
${SETTING_VARIABLE} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment