pixelb / crudini

A utility for manipulating ini files
GNU General Public License v2.0
443 stars 60 forks source link

read complete ini file in shell format #41

Closed thorstenkampe closed 1 year ago

thorstenkampe commented 8 years ago

Does crudini have support for reading the whole content (sections, keys) in one read with --format sh?

The documentation suggests I have to iterate over all sections in order to do that. That is inefficient and time consuming because the ini file is read (opened, closed) for each iteration...

pixelb commented 8 years ago

--format=lines was added to give general flexibility like that. With some caveats on the format of the values, something like this might suffice, to give unique section_param=value items:

eval $(crudini --format=lines --get file.ini | sed 's/\[ \(.*\) \]/\1/; s/ = /=/; s/ /_/g')

This might be awkward/useful enough to warrant a --format=lines-sh option. Actually it probably makes sense for --format=sh to do that automatically when not presented with a section, as the current mode of just listing sections is not too useful.

Another interesting option is to have --format=bash output an associative array. While those can't be nested you could do something like:

declare -A filename_ini
filename_ini[section1|name1]=value11
filename_ini[section 2|name1]=value21
filename_ini[section 2|name2]='value2 2'

Which could be accessed like:

echo ${filename_ini[section 2|name2]}