Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using -f operator on a string that contains curly braces

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?

like image 257
Matt Avatar asked Aug 21 '14 21:08

Matt


People also ask

What is the use of {} in Python?

In languages like C curly braces ( {} ) are used to create program blocks used in flow control. In Python, curly braces are used to define a data structure called a dictionary (a key/value mapping), while white space indentation is used to define program blocks.

What is {} used for in Java?

In a Java program, everything is subordinate to the top line — the line with class in it. To indicate that everything else in the code is subordinate to this class line, you use curly braces. Everything else in the code goes inside these curly braces.

How do you put curly brackets in string?

If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }} . So if you want to print "{42}", you'd use "{{{0}}}".

What is the use of curly braces in SQL?

The clauses in an SQL statement that display between [brackets] are optional. If an optional clause has several components or keywords, they display within the brackets. Curly braces {} in SQL statements indicate that one or more clauses are used together.


1 Answers

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
        }
like image 64
Alexander Obersht Avatar answered Sep 22 '22 10:09

Alexander Obersht