如何在Linux中读取INI文件?

时间:2021-02-10 22:28:58

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

如何使用Linux命令读取INI文件?我知道在Windows中你可以使用像GetPrivateProfileString这样的API调用。

Example; how to get version under system2:

例子;如何在系统2下获得版本:

[system1]

version=XYZ

date=123

[system2]

version=ABC

date=985

2 个解决方案

#1


1  

You might be interested in the python module ConfigParser:

您可能对python模块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:

作为一个脚本pass_config.py:

import ConfigParser

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

Run:

运行:

$ python pass_config.py
ABC

#2


1  

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

看看crudini,它是一个专门的工具来从shell中操作ini文件吗

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

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

有关使用和下载的详细信息:http://www.pixelbeat.org/programs/crudini/

#1


1  

You might be interested in the python module ConfigParser:

您可能对python模块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:

作为一个脚本pass_config.py:

import ConfigParser

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

Run:

运行:

$ python pass_config.py
ABC

#2


1  

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

看看crudini,它是一个专门的工具来从shell中操作ini文件吗

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

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

有关使用和下载的详细信息:http://www.pixelbeat.org/programs/crudini/