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

查看:52
本文介绍了简单的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的格式,并带有#; 作为注释,并带有 option = value .

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 install 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).

示例: change-config/etc/default/nginx ULIMIT'"-n 4096"'

预期结果将是在nginx文件中包含 ULIMIT ="-n 4096" .显然,如果它确实已经存在并且具有相同的值,那么它应该什么也不做.如果存在的话,评论旧行会很好,然后添加新行.

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.

推荐答案

git config 实际上是一个半通用的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天全站免登陆