bash_ini_parser
bash_ini_parser copied to clipboard
feature request, allow special characters for section and var names
subject changed by Rudi to match the feature request of post #2
sorry , i use ./read_ini.sh to run it instead of .read_ini , and i cant close this issue. but i just use it to read application.ini which has a section [staging : production] , then the function exit with Error: Invalid line: 58: [staging : production] Can we built a more smart lib to deal with this ?
Allowing such special characters is not easy possible because read_ini's interface wants to use these strings as bash variables. However if you check out the next branch you can try another interface using bash associative arrays (requires bash >=4.0). I'd written this 2 years ago but the documentations is not up-to-date yet. It should work like this:
$ cat /tmp/bla.ini a="xyz" b="uvw" [staging : production] a="mnl"
$ read_ini /tmp/bla.ini $ echo "${INI["a"]}" xyz $ echo "${INI["a"]}" xyz $ echo "${INI["b"]}" uvw $ echo "${INI["staging : production__a"]}" mnl
hope that helps, Rudi
Forgot to note, you need to declare $INI before calling read_ini: unset INI declare -A INI read_ini test1.ini
see also the examples in the test directory.
Thank your for reminding :P , i will try to use it .
Would it perhaps be an option to be less strict when reading the sections and values, and just convert special characters to underscore before creating the variables? This is already being done for '.' correct?
https://github.com/rudimeier/bash_ini_parser/blob/8fb95e3b335823bc85604fd06c32b0d25f2854c5/read_ini.sh#L232-237
I switched to using the 'next' branch which supports special characters in the section names.
However I came across a complex example in one of the configuration files that I am parsing that is still not handled as expected:
enabled = true
# top-level directories for browsing, as <name> <uri>
directories =
Albums local:directory?type=album
Artists local:directory?type=artist
Composers local:directory?type=artist&role=composer
Folders local:directory:
Genres local:directory?type=genre
Performers local:directory?type=artist&role=performer
Release Years local:directory?type=date&format=%25Y
Tracks local:directory?type=track
Last Week's Updates local:directory?max-age=604800
Last Month's Updates local:directory?max-age=2592000
# database connection timeout in seconds
timeout = 10
In this example, each line is handled as a separate parameter value pair instead of forming part of directories. The script also fails on Folders local:directory:, expecting an '=' at the end. Any ideas on how to deal with the above?
I've created pull request https://github.com/rudimeier/bash_ini_parser/pull/9 to add support for multiline values, and added a few other features similar to what can be found in Python's RawConfigParser
.
All of the tests still run and I've added a few new ones. @rudimeier will you please review?