对包含大括号的字符串使用-f运算符 [英] Using -f operator on a string that contains curly braces

查看:293
本文介绍了对包含大括号的字符串使用-f运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ blankDefinition = @这个字符串表示我想要为我的Nagios定义使用一个模板。 
定义主机{
使用windows-server;从模板继承默认值
host_name {0};我们给这个主机的名字
别名{0};与主机
地址{1}相关联的较长名称;主机的IP地址
}

@

我有一个脚本来决定哪个服务器不在nagois中。因为它循环我试图让它吐出一个definion为我,以便我可以复制并粘贴到我的Nagios配置。

  $比较| %{
$ blankDefinition -f $($ _。serverName),$($ _。ipAddress)
}

哪个网上有以下错误:

 字符串格式错误:输入字符串不在一个正确的格式.. 

类似这样的工作很好

  @
测试
一个
{0}
三个
@ -f十二个$ b $ b

然后我发现这是因为其他花括号。有没有办法,我可以格式化我的字符串使用-f保留大括号?或者我必须只使用变量替换?

解决方案

添加另一组大括号似乎工作:

  $ blankDefinition = @
定义主机{{
使用windows-server;继承模板的默认值
host_name {0};我们给这个主机的名字
别名{0};与主机
地址{1}关联的更长的名字;主机的IP地址
} }
$ b $ @

$ blankDefinition -f'foo','bar','foo'









$ b使用windows-server ;继承模板的默认值
host_name foo;我们给这个主机的名字
别名foo;与主机
地址栏关联的较长名称;主机的IP地址
}


Consider the following here-string that I am trying to use a template for my Nagios definitions.

$blankDefinition = @"
define host{
        use             windows-server  ; Inherit default values from a template
        host_name       {0}       ; The name we're giving to this host
        alias           {0}       ; A longer name associated with the host
        address         {1}     ; IP address of the host
        }

"@

I have a script that determines which of my servers are not in nagois that should be. As it loops I'm trying to have it spit out a definion for me so that I can copy and paste into my Nagios config.

$comparison | %{
    $blankDefinition -f $($_.serverName),$($_.ipAddress)
}   

Which nets me the following error:

Error formatting a string: Input string was not in a correct format..

Something like this works just fine

@"
Testing
One
{0}
Three
"@ -f "Twelve"

So then I found out that it is because of the other curly braces. Is there a way that i can format my string using the -f while keeping the braces? Or do i have to use variable subsitution only?

解决方案

Adding another set of curly braces seems to work:

$blankDefinition = @"
define host{{
        use             windows-server  ; Inherit default values from a template
        host_name       {0}       ; The name we're giving to this host
        alias           {0}       ; A longer name associated with the host
        address         {1}     ; IP address of the host
        }}

"@

$blankDefinition -f 'foo', 'bar', 'foo'

Output:

define host{
        use             windows-server  ; Inherit default values from a template
        host_name       foo       ; The name we're giving to this host
        alias           foo       ; A longer name associated with the host
        address         bar     ; IP address of the host
        }

这篇关于对包含大括号的字符串使用-f运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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