简单的Linux命令行命令来修改在INI像配置文件选项 [英] Simple linux command line command to modify an option in a INI like config file

查看:1082
本文介绍了简单的Linux命令行命令来修改在INI像配置文件选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一个通用的命令行的解决方案,可以让我添加或修改配置文件中的配置选项(INI格式一样)。

I am looking for a generic command line solution that would allow me to add or modify a configuration option inside a config file (ini like format).

大多数Linux配置文件使用INI格式一样,以#和; (注释)和选项=值。

Most linux configuration files use a INI like format, with # and ; as comment and with option=value.

主要是我要寻找的东西,将需要的文件名,选择和价值,这将改变配置来匹配这​​一点。

Mainly I am looking for something that would take filename, option and value, and that will change the config to match this.

我想用这个来写一些自动部署脚本。我有使用默认情况下未在Debian或Ubuntu安装的工具没有问题的,只要它们在默认的分销仓库存在(因为我可以做的apt-get安装XXX,如果需要的话)。

I want to use this to write some auto-deployment scripts. I have no problem on using tools that are not installed by default on Debian or Ubuntu as long they do exist in the default distribution repositories (as I can do an apt-get install xxx, if needed).

例如:转换配置的/ etc /默认/ nginx的ULIMIT'-n 4096'

预期的结果将是有 ULIMIT = - N 4096 Nginx的文件中。显然,如果没有已经存在,并且具有相同的值,它应该什么都不做。如果它存在,评论旧线将被罚款并添加新的。

The expected result would be to have ULIMIT="-n 4096" inside the nginx file. Obviously if it does already exists and have the same value, it should do nothing. If it exists, commenting the old line would be fine and adding the new one.

作为一个说明,这些配置文件可以有空格/参数之间的标签,所以如果你有 ULIMIT =...仍然是相同的命令。这就是为什么我一直在寻找更好的东西比sed的,因为有很多的边界情况来评估。

As a note, these config files can have spaces/tabs between parameters so if you have ULIMIT = "..." is still the same command. That's why I was looking for something better than sed as there are plenty of corner cases to evaluate.

另外,我并不想另起炉灶,我怀疑,我要寻找一个解决此类问题的第一个。

Also, I don't want to reinvent the wheel, and I doubt that I am the first one to look for a solution to this kind of problem.

推荐答案

混帐配置 实际上是一个半通用INI接口。

git config is actually a semi-generic INI interface.

❱ git config --file=/etc/default/nginx somegroup.ULIMIT '-n 4096'
❱ cat /etc/default/nginx
[somegroup]
    ULIMIT = -n 4096
❱ git config --file=/etc/default/nginx somegroup.ULIMIT
"-n 4096"

它不支持添加顶级键,虽然。所有按键都被放在一个INI风格组中,因而有somegroup。以上。这使得它不适合你的任务,但我想我会在这里提到它为他人在这里找到自己的方式。

It doesn't support adding top-level keys, though. All keys have to be placed in an INI style group, hence the "somegroup." above. That makes it unsuitable for your task, but I thought I'd mention it here for others finding their way here.

这篇关于简单的Linux命令行命令来修改在INI像配置文件选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆