1

I have one ini configuration file, I need to create a shell script using this configuration. What is the easiest method to access all variable, Can be used effectively from the shell script.

Can I use an array or something? Now planing to find the count of [] brackets then through awk get all variables one by one. Please suggest if any easiest way to effectively

cat app.ini 

Below the output of my sample configuration file. Can be N no of Blocks.

[APP1]
name=Application1
StatusScript=/home/status_APP1.sh
startScript=/home/start_APP1.sh
stopScript=/home/stop_APP1.sh
restartScript=/home/restart.APP1.sh
logdir=/log/APP1/

[APP2]
name=Application2
StatusScript=/home/status_APP2.sh
startScript=/home/start_APP2.sh
stopScript=/home/stop_APP2.sh
restartScript=/home/restart.APP2.sh
logdir=/log/APP2/
.
.
.
.
.
[APPN]
name=ApplicationN
StatusScript=/home/status_APPN.sh
startScript=/home/start_APPN.sh
stopScript=/home/stop_APPN.sh
restartScript=/home/restart.APPN.sh
logdir=/log/APPN

/

1
  • You want to use one [APP] section at a time to pass some variables to another command? Can you show at least in pseudocode what the calling code looks like? Commented Mar 2, 2018 at 7:25

2 Answers 2

1

Consider using a library like bash-ini-parser https://github.com/albfan/bash-ini-parser. It covers a lot of nuances like indentation, whitespaces, comments etc.

The example for your case may look like this:

 #!/bin/bash

. bash-ini-parser

cfg_parser app.ini

cfg_section_APP1
echo $name

cfg_section_APP2
echo $logdir

cfg_section_APPN
echo $logdir
Sign up to request clarification or add additional context in comments.

3 Comments

If you are reading in variables from an external source, you should definitely use double quotes around their values when interpolating them.
I would be leery of the linked parser; the readme itself describes it as " more a hack than a reliable parser".
Very Good option, Nicely organised :)
0

Below line help us to locate particular values in each section.

sed -nr "/^\[APP1\]/ { :l /^name[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" app.ini

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.