user1179317
user1179317

Reputation: 2913

How to read INI file in Linux

How would you read an INI file using Linux commands? I know in Windows you can use API calls like GetPrivateProfileString..

Example; how to get version under system2:

[system1]

version=XYZ

date=123

[system2]

version=ABC

date=985

Upvotes: 2

Views: 8866

Answers (2)

pixelbeat
pixelbeat

Reputation: 31728

Have a look at crudini which is a dedicated tool to manipulate ini files from shell

version=$(crudini --get example.ini system2 version)

Details on usage and download at: http://www.pixelbeat.org/programs/crudini/

Upvotes: 3

Chris Seymour
Chris Seymour

Reputation: 85815

You might be interested in the python module ConfigParser:

In [1]: import ConfigParser

In [2]: config = ConfigParser.ConfigParser()

In [3]: config.read('file.ini')
Out[3]: ['file.ini']

In [4]: config.get('system2','version')
Out[4]: 'ABC'

As a script pass_config.py:

import ConfigParser

config = ConfigParser.ConfigParser()
config.read('file.ini')
print config.get('system2','version')

Run:

$ python pass_config.py
ABC

Upvotes: 1

Related Questions