2

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
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
user1179317
  • 2,693
  • 3
  • 34
  • 62

2 Answers2

2

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/

pixelbeat
  • 30,615
  • 9
  • 51
  • 60
1

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
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202