Skip to content
Advertisement

Parsing a config file inside shell script

I am trying to parse an ini file inside a shell script.

name.conf file:

JavaScript

I could parse the values by the command

JavaScript

I wanted to know if there was some way by which I could group the contents of my config and parse it

name.conf file:

JavaScript

I just need the values in a shell array say names and print them like this

JavaScript

Advertisement

Answer

It’s easier than that. Just put all vars in a file (“vars”, for instance), in “name = value” format:

JavaScript

Then in your main script, use “source” to load it:

JavaScript

Or using period also works:

JavaScript

Although that will only work if you run the script where the vars file is. Nagios use this approach to be able to run the script anywhere in the system:

JavaScript

The output for that should be ‘abc’

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement