bash 使用bash更新ini文件中的var

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16987648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:38:14  来源:igfitidea点击:

Update var in ini file using bash

bash

提问by Ben Swinburne

I am attempting to write a bash script to configure various aspects of a server. The context here is replacing a value of a variable in a conf file (ini format) with another value.

我正在尝试编写一个 bash 脚本来配置服务器的各个方面。这里的上下文是用另一个值替换 conf 文件(ini 格式)中的变量值。

The context is

上下文是

[ssh-iptables]

enabled = false

And I simply need to change false to true.

我只需要将 false 更改为 true。

Typically I'd just do this with a simple bit of sed

通常我只会用一些简单的东西来做到这一点 sed

sed -i 's/^enabled = false/enabled = true/g' /etc/fail2ban/jail.conf

But enabled = falseexists in multiple places.

enabled = false存在于多个地方。

I've tried using awkwith no success

我试过使用但awk没有成功

awk -F ":| " -v v1="true" -v opt="enabled" ' == "[ssh-iptables]" && !f {f=1}f &&  == opt{sub("=.*","= "v1);f=0}1' /etc/fail2ban/jail.conf

The above was sourced from this forum threadbut I don't really have enough understanding of how to use it in scripts to make it work. All it seems to do is the equivalent of cat /etc/fail2ban/jail.conf

以上内容来自此论坛帖子,但我对如何在脚本中使用它以使其工作并没有足够的了解。它似乎所做的一切都相当于cat /etc/fail2ban/jail.conf

I have found a few other scripts which are considerably longer which isn't ideal as this will happen to loads of ini files so I'm hoping someone can help me correct the above code or point me in the right direction.

我发现了一些其他脚本,它们相当长,这并不理想,因为这会发生在大量 ini 文件中,所以我希望有人可以帮助我更正上述代码或指出正确的方向。

Apologies if this belongs on ServerFault, but as it's scripting rather than the intricacies of server configuration itself I figured here might be more apt.

抱歉,如果这属于 ServerFault,但由于它是脚本而不是服务器配置本身的复杂性,我认为这里可能更合适。

回答by Dan Fego

Assuming your format is that there are no square-bracket lines (like [ssh-iptables]) within sections, I would use your solution above (with sed) but restrict the query to within that block like so:

假设您的格式是部分内没有方括号线(如[ssh-iptables]),我将使用上面的解决方案(使用sed),但将查询限制在该块内,如下所示:

sed -i '/^\[ssh-iptables\]$/,/^\[/ s/^enabled = false/enabled = true/' /etc/fail2ban/jail.conf

The extra part at the beginning tells the following substitution statement to only run between the line that is [ssh-iptables]and the next one that starts with a [. It uses two regular expressions separated by a comma which indicate the bounds.

开头的额外部分告诉下面的替换语句只在 is[ssh-iptables]和下一个以 a 开头的行之间运行[。它使用两个用逗号分隔的正则表达式来表示界限。

回答by Lauskin

If you are open to use external applications, you could be interested into the use of crudini.

如果您愿意使用外部应用程序,您可能会对使用crudini.

Example:

例子:

[oauth2provider]
module = SippoServiceOAuth2Provider
backend[] = none
wiface = public

; [calldirection]
; module = SippoServiceCallDirection
; backend[] = none
; wiface = internal

A standard grep will not filter commented exceptions.

标准 grep 不会过滤注释异常。

With crudinithings for consulting, setting and modify are easier:

有了crudini咨询的东西,设置和修改更容易:

$ crudini --get /myproject/config/main.ini oauth2provider wiface
public
$ crudini --get /myproject/config/main.ini calldirection wiface
Section not found: calldirection

I was on a bash-only app and moved to this approach. Just a suggestion.

我在一个仅限 bash 的应用程序上并转向这种方法。只是一个建议。

Regards,

问候,

回答by PaulProgrammer

You might consider using m4 instead of sed in this case. This uses variable replacement and I think keeps the file looking readable. Your m4 template might look like this:

在这种情况下,您可以考虑使用 m4 而不是 sed。这使用变量替换,我认为使文件看起来可读。您的 m4 模板可能如下所示:

[ssh-iptables]
enabled=SSH_IPTABLES_ENABLED

Now, you call m4 with the following parameters (which can be called from a bash script):

现在,您使用以下参数调用 m4(可以从 bash 脚本调用):

m4 -DSSH_IPTABLES_ENABLED=true input.m4 > output.ini

or:

或者:

m4 -DSSH_IPTABLES_ENABLED=false input.m4 > output.ini

This is an overly simple way of using m4, if you read about it you'll find you can do some really nifty things (this is the infrastructure upon which autoconf/automake was initially designed).

这是使用 m4 的一种过于简单的方法,如果您阅读它,您会发现您可以做一些非常漂亮的事情(这是最初设计 autoconf/automake 的基础架构)。

回答by jim mcnamara

awk '/^[ssh-iptables/ {ok=1}
     ok==1 && ##代码##="enabled = false" {print "  enabled = true"; ok=0 ; next}
     {print ##代码##} ' infile > tmp
     mv tmp infile