On Sunday, December 13, 2015 at 10:20:00 AM UTC-6, Vadym Chepkov wrote:
>
> Hi,
>
> I can't figure out how to include a single quoted string into a command, 
> not matter what escaping I do.
> Here is a simplified code :
>
> $string = '\'a quoted string\''
>
> exec { 'show string':
>   command   => "echo \'I want to see ${string}\'",
>   logoutput => true,
>   path      => '/bin:/usr/bin',
> }
>
> Quotes don't show up, no matter what.
> What do I do wrong?
>


The value of variable $string starts and ends with a single quote.  These 
are retained when you insert it into the Exec's 'command' paramater, so 
that there are then four single quotes in that parameter value (and no 
backslashes) in that parameter's value.  You are using the default provider 
for your Exec (probably the 'posix' provider), so this command string ends 
up being handed off to Ruby's Kernel.exec(), which under some circumstances 
hands it off to the shell, and under other circumstances performs its own 
expansions as if by the shell, and executes it directly.  So consider what 
the shell would do with the resulting command string:

echo 'I want to see 'a quoted string''

It is equivalent to this:

echo "I want to see a" quoted string

And of course, the output is just

I want to see a quoted string

There are several ways to go about what you are after but the bottom line 
is that you need to recognize that the strings will be processed at least 
twice: once by Puppet, and once by Kernel.exec(), in that order.  You need 
to insert appropriate escapes or quotes into your command for the latter, 
and then escape or quote the result for the former.
 
For example, you might do this:

$string = "\\'a quoted string\\'"

exec { 'show string':
  command   => "echo I want to see ${string}",
  logoutput => true,
  path      => '/bin:/usr/bin',
}

That escapes the backslashes so they get passed on to Kernel.exec() or the 
shell, and uses double quotes around the value of $string so that the 
single quotes within do not need to be escaped for Puppet.  There are other 
ways to wrangle the details, of course.


John

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/94110d9f-8b50-43bd-8038-59ee591d5c23%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to