I have a parameters.ini file, such as:
[parameters.ini]
database_user = user
database_version = 20110611142248
I want to read in and use the database version specified in the parameters.ini file from within a bash shell script so I can process it.
#!/bin/sh
# Need to get database version from parameters.ini file to use in script
php app/console doctrine:migrations:migrate $DATABASE_VERSION
How would I do this?
Sed one-liner, that takes sections into account. Example file:
Say you want param2 from section2. Run the following:
will give you
Some of the answers don't respect comments. Some don't respect sections. Some recognize only one syntax (only ":" or only "="). Some Python answers fail on my machine because of differing captialization or failing to import the sys module. All are a bit too terse for me.
So I wrote my own, and if you have a modern Python, you can probably call this from your Bash shell. It has the advantage of adhering to some of the common Python coding conventions, and even provides sensible error messages and help. To use it, name it something like myconfig.py (do NOT call it configparser.py or it may try to import itself,) make it executable, and call it like
Here's my code for Python 3.5 on Linux:
one of more possible solutions
All of the solutions I've seen so far also hit on commented out lines. This one didn't, if the comment code is
;
:This uses the system perl and clean regular expressions:
Just include your .ini file into bash body:
File example.ini:
File example.sh