On Wed, Jan 5, 2011 at 12:40 PM, Alan Barrett <a...@cequrux.com> 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 puppet-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> puppet-users+unsubscr...@googlegroups.com<puppet-users%2bunsubscr...@googlegroups.com>
> .
> 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 puppet-us...@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en.

Reply via email to