Escaping Quotes

3,526 views
Skip to first unread message

Douglas Garstang

unread,
Nov 21, 2011, 1:30:37 AM11/21/11
to Puppet Users
Grrr. I have the exec{} below in my puppet module. How do I escape the
\ characters? I've tried every possible combination I can think of.
I've used one, I've used two, and I've used THREE \.

exec {
'oracle-extract-part':
command => "/usr/bin/printf
\"n\np\n2\n2091\n+16384M\nw\n\" | /sbin/fdisk /dev/xvdj",
unless => "/bin/cat /proc/partitions | /bin/grep
${orcl_ephm_device}2";
}

With three \, it ends up looking like this in the log:

Nov 21 01:27:45 dev-c3-app-15 puppet-agent[3091]:
(/Stage[main]/Oracle::Server11g/Exec[oracle-swap-part]/returns) change
from notrun to 0 failed: /usr/bin/printf
\"n\#012p\#0121\#0121\#012+32768M\#012t\#01282\#012w\#012\" |
/sbin/fdisk /dev/xvdj returned 1 instead of one of [0] at
/etc/puppet/devmp/modules/oracle/manifests/server11g.pp:136

Now... that's obviously not right. How do I escape \ symbols?

Doug.

Peter Meier

unread,
Nov 21, 2011, 6:14:33 AM11/21/11
to puppet...@googlegroups.com
> exec {
> 'oracle-extract-part':
> command => "/usr/bin/printf
> \"n\np\n2\n2091\n+16384M\nw\n\" | /sbin/fdisk /dev/xvdj",
> unless => "/bin/cat /proc/partitions | /bin/grep
> ${orcl_ephm_device}2";
> }

if you don't need to interpolate a variable, you can simply use single
quotes, so you don't need to escape anything.

grrr ~pete

Phil Frost

unread,
Nov 21, 2011, 7:55:49 AM11/21/11
to puppet...@googlegroups.com
On 11/21/2011 01:30 AM, Douglas Garstang wrote:
> Grrr. I have the exec{} below in my puppet module. How do I escape the
> \ characters? I've tried every possible combination I can think of.
> I've used one, I've used two, and I've used THREE \.
>
> exec {
> 'oracle-extract-part':
> command => "/usr/bin/printf
> \"n\np\n2\n2091\n+16384M\nw\n\" | /sbin/fdisk /dev/xvdj",
> unless => "/bin/cat /proc/partitions | /bin/grep
> ${orcl_ephm_device}2";
> }

Sounds like you need to use FOUR. Say you wanted to use printf to print
a newline. You would run this:

(1) printf \n

But, the backslash has special meaning in bash (or most shells). If you
type that in bash, you have to escape it, one of these ways:

(2) printf \\n
(3) printf '\n'

Puppet runs commands through a shell. So, you will have to apply
escaping appropriate for whatever shell is in use.

But, the command is also in a string. Backslashes have special meaning
in puppet strings. So, we know the command we want to give to bash is:

(4) printf \\n

To put that in a puppet string we need to escape the \:

(5) "printf \\\\n"

If you don't like all the backslashes, you could give single quotes to bash:

(6) "printf '\\n'"

This is example 3, quoted with puppet's rules. You could also use single
quotes in Puppet, but Puppet's single-quote strings still find special
meaning in \, so it doesn't really get you anything, and if you are
using single quotes for bash it's actually worse. You'd need this:

(7) 'printf \'\\n\''

Given (7), puppet will give (8) to bash:

(8) printf '\n'

and bash will give to exec():

(9) printf \n

Make sense?

Max Schubert

unread,
Nov 21, 2011, 8:13:25 AM11/21/11
to puppet...@googlegroups.com
How about using the alternate single quotes in ruby

%q{string string string}

Max

> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To post to this group, send email to puppet...@googlegroups.com.
> To unsubscribe from this group, send email to
> puppet-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/puppet-users?hl=en.
>
>

Peter Meier

unread,
Nov 21, 2011, 8:49:23 AM11/21/11
to puppet...@googlegroups.com
Zitat von Max Schubert <max.sc...@gmail.com>:

> How about using the alternate single quotes in ruby
>
> %q{string string string}

puppet manifests are not ruby code.

~pete

Max Schubert

unread,
Nov 21, 2011, 9:07:22 AM11/21/11
to puppet...@googlegroups.com
Right - forgot that :). Maybe support for an alternate quoting
operator should be added.

Mike Pountney

unread,
Nov 21, 2011, 6:12:20 AM11/21/11
to puppet...@googlegroups.com

I'd deploy a wee script that does what you are trying to handle in the command string, and just call that instead.

Corby

unread,
Jul 24, 2012, 3:09:48 PM7/24/12
to puppet...@googlegroups.com
I placed the printf string in a separate variabe, then I can double quote the
variable and it will take care of escaping correctly.

$fdisk_cmd = "c\nu\nn\np\n1\n\n\nw\n"
exec { "fdisk /dev/${name}":
command => "printf \"$fdisk_cmd\" | fdisk /dev/${name}",
unless => "cat /proc/partitions | grep ${name}1",
path => '/sbin:/bin:/usr/bin',
}




Reply all
Reply to author
Forward
0 new messages