On Wed, Jan 5, 2011 at 12:40 PM, Alan Barrett <[email protected]> wrote:
> On Wed, 05 Jan 2011, Nigel Kersten wrote:
> > define appendifnosuchline($file="", $line="") {
> > exec { "appendline_${file}_${line}":
> > path => "/bin:/usr/bin",
> > command => "/bin/echo ${line} >> ${file}",
> > unless => "grep -qx ${line} ${file}",
> > }
> > }
> >
> > appendifnosuchline { "ensure_foobar_in_filetest":
> > file => "/tmp/filetest",
> > line => "foobar",
> > }
> >
> > That looks to work.
>
> ... unless $line begins with "-" (which will confuse grep or some
> versions of echo), or contains space or other shell special characters
> (which will confuse the shell), or contains backslash (which is special
> to some versions of /bin/echo), or contains regexp secial characters
> (which will confuse grep).
>
All excellent points.
>
> Here's my untested attempt:
>
> define appendifnosuchline($file, $line) {
> $escaped_line = shellquote($line)
> exec { "appendline_${title}":
> path => "/bin:/usr/bin",
> command => "printf '%s\\n' ${escaped_line} >> ${file}",
> unless => "grep -qFx -e ${escaped_line} ${file}",
> }
> }
>
> This is untested, and still doesn't handle spaces or shell special
> characters in the file name, but it attempts to deal with the other
> issues:
>
> * Escape shell special characters in $line;
> * Use printf instead of echo to avoid portability problems with
> different versions of echo interpreting "-" or "\" differently.
> * Use grep -F option to make it search for a fixed string
> instead of a regular expression;
> * Use grep -e option to avoid problems if $line begins with "-";
>
> I also made the $file and $line parameters compulsory, and derived the
> title of the exec from the title of the appendifnosuchline instead of
> from the file name and line contents (which might be long or ugly).
>
That's a good improvement.
>
> --apb (Alan Barrett)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<puppet-users%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/puppet-users?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/puppet-users?hl=en.