How do I get value of variable from file using shell script?

▼魔方 西西 提交于 2019-12-19 10:17:04

问题


There is a file post_check.ini I need the value set for:

Max_value=2

How would I get the value 2 from Max_value?


回答1:


try

grep -Po '(?<=Max_value=).*' post_check.ini



回答2:


Max_value=$(sed -n '/^Max_value=\([0-9]*\)$/s//\1/p' post_check.ini)



回答3:


I recommend using crudini which is a dedicated tool to manipulate ini files from shell

value=$(crudini --get /usr/post_check.ini section Max_value)

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




回答4:


You might find it useful to use proper config file parser. Given the following .ini file:

$ cat post_check.ini
[section 1]
Max_value=123
[section 2]
Max_value=456

The following python script will print 123:

import ConfigParser, os
config = ConfigParser.ConfigParser()
config.read('post_check.ini')
print config.get('section 1','Max_value')

This is most reliable and modifiable way to go if you need to work with config files.



来源:https://stackoverflow.com/questions/15793452/how-do-i-get-value-of-variable-from-file-using-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!