简体   繁体   English

在linux中读取属性文件

[英]Reading a properties file in linux

I want to be able to read a properties file in linux, check for if certain properties exist. 我希望能够在linux中读取属性文件,检查是否存在某些属性。 If the properties I am looking for exist but they dont match the value I am looking for I want to override those respective properties. 如果我正在寻找的属性存在,但它们与我正在寻找的值不匹配,我想覆盖那些相应的属性。 If they dont exist then I want to write them to the file. 如果它们不存在,那么我想将它们写入文件。

Can any linux guru help me out with this. 任何Linux大师都可以帮助我解决这个问题。

Thanks in advance! 提前致谢!

Also the key names can be in the form pre1.pre2.pre3 so something like pre1.pre2.pre3 = value 键名也可以是pre1.pre2.pre3的形式,所以类似于pre1.pre2.pre3 = value

The following is the properties file 以下是属性文件

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.all.disable_ipv6 = 1
kernel.sysrq = 0
net.ipv4.ip_forward = 0
net.ipv4.tcp_syncookies = 1
net.ipv6.conf.all.forwarding = 0
net.ipv4.tcp_keepalive_time = 30
net.ipv4.tcp_keepalive_intvl = 5
net.ipv4.tcp_keepalive_probes = 5

I want to change all the settings for tcp basically. 我想基本上改变tcp的所有设置。

Here are some steps that you might find useful for your script: 以下是您可能会发现对脚本有用的一些步骤:

  • does foo.bar exist in a.properties? foo.bar是否存在于a.properties中?

if grep foo.bar a.properties ; then echo found ; else echo not found; fi

-> if tests the outcome of grep - > if测试grep的结果

  • replace the property with it's new value 用它的新值替换属性

cat a.properties | sed '/foo.bar/c\\foo.bar = the new value'

-> sed with the c command changes a whole line - >用c命令sed改变整行

Looks like the last command is all you need :) 看起来最后一个命令就是你所需要的:)

ps: I love these 'avoid bash' discussions :) ps:我喜欢这些'避免bash'的讨论:)

#!/bin/sh
ensure_value() {
    file="$1"
    property="$2"
    value="$3"
    if ! grep -q "$property" "$file"; then
        echo $property = $value >> "$file"
    else
        sed -i -e "s/^$property.*/$property = $value/g" "$file"
    fi
}

# cp props props.$(date +%s).bak
ensure_value props net.ipv6.conf.all.disable_ipv6 1
ensure_value props net.ipv4.ip_forward 1
# etc.

This script is not safe or production-ready! 此脚本不安全或生产就绪! Note that the function ensure_value evaluates regexs in property names and it can go horribly wrong if your property ends up being something like .* . 请注意,函数ensure_value评估属性名称中的正则表达式,如果您的属性最终类似于.* ,则可能会出现严重错误。 Really, you should use Ansible's INI file module or similar instead. 真的,你应该使用Ansible的INI文件模块或类似的。

Firstly, I'd like to point out that modifying properties files via scripts can be quite dangerous so please be careful when you are testing this. 首先,我想指出通过脚本修改属性文件可能非常危险,因此在测试时请小心。 I can think of two options that would get you what you want. 我可以想到两个可以让你得到你想要的选择。

  1. If you want just a defaults file which won't change and simply want to replace what you have with a defaulted file, then you can keep a default version of the file on hand and do a simple diff between the properties file and your default version. 如果您只想要一个不会更改的默认文件,只想更换默认文件,那么您可以保留文件的默认版本并在属性文件和默认版本之间进行简单的区分。 If the files differ, copy the default to the actual file. 如果文件不同,请将默认值复制到实际文件。

  2. Since I suspect that option 1 is not viable for you, I would look into doing sed replacements in the file. 由于我怀疑选项1对您不可行,我会考虑在文件中进行sed替换。 There are plenty of tutorials on how to edit files with sed out there on the internet. 有很多关于如何在互联网上编辑sed文件的教程。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM