-
-
Save splaspood/1473761 to your computer and use it in GitHub Desktop.
cfg.parser () { | |
fixed_file=$(cat $1 | sed 's/ = /=/g') # fix ' = ' to be '=' | |
IFS=$'\n' && ini=( $fixed_file ) # convert to line-array | |
ini=( ${ini[*]//;*/} ) # remove comments | |
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[*]/%\( \)/\(\) \{} ) # 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 '/etc/my.ini' | |
cfg.section.images |
Excelent. Do you mind if I improve it and add to my colection of Bash utilities ?
hey
could you say anthing about the license?
hey
could you say anthing about the license?
bump
A license is a good question... I'm pretty sure this function came from someone else's script that I probably polished up.. it's been so long I can't recall. So as far as I'm concerned, I'm happy to let anyone use this as they see fit with no requirements from me.. I'd probably just go for the MIT license if I had to pick one off the cuff. However, as it's likely lifted in whole or in part from someone else's work, I'm probably not in a position to spec my own license.. Update: Did a quick google, looks like I probably got it from here, or the original blog post that's referenced there (which now seems defunct) https://stackoverflow.com/questions/6318809/how-do-i-grab-an-ini-value-within-a-shell-script
Specifically,
This answer: https://stackoverflow.com/a/6318901/2110294
This blog post: https://web.archive.org/web/20180124065211/theoldschooldevops.com/2008/02/09/bash-ini-parser/
Here is the authors details:
Andrés J. Díaz
https://www.linkedin.com/in/andresjdl
https://twitter.com/andresjdiaz
https://github.com/ajdiaz
https://ajdiaz.me/
I've mailed him to ask if he can't possibly create a repository for this with a license.
Wonderful! I'll happily take this down if he would like.
Hi @splaspood,
I am using it in script with #!/bin/sh
and i get error , `cfg.parser': not a valid identifier
How can i use it in script with #!/bin/sh
Those interested in even modifying the INI file can refer to this one: https://github.com/albfan/bash-ini-parser which is an enhancement from the same original source. This is under GPL v3.
Other options:
https://github.com/rudimeier/bash_ini_parser
https://github.com/pixelb/crudini
Pleased that people keep finding this gist useful. Thanks @techpavan for the updated source!
this is wonderful :) 👍