Re: sudo question
On Thu, Nov 21, 2024 at 09:48:06 +0100, tomas@tuxteam.de wrote:
> On Thu, Nov 21, 2024 at 08:32:30AM +0000, Michael Kjörling wrote:
> > On 20 Nov 2024 17:49 -0500, from greg@wooledge.org (Greg Wooledge):
> > >> sudo echo "something" >>/etc/postfix/virtual_alias_maps
> > >
> > >> Can you help me why the first sudo failed?
> > >
> > > The redirection >> is being done before sudo is executed.
> >
> > Indeed.
> >
> > The usual pattern if you need to do this in a non-root-only script is
> > to do something like `echo | sudo tee` to tie the sudo to the thing
> > that needs write access to the output file.
>
> My favourite is actually "sudo dd of=<file>" it hasn't the side effect
> of flooding your stdout (esp. with a larger, uglier thing).
Typically you redirect tee's output to /dev/null.
Since the OP wanted to append, "tee -a" is a viable choice, but POSIX dd
doesn't have an append option.
Checking my local Debian man pages now, however, I see that Debian's dd
(GNU coreutils) *does* offer an append option.
dd oflag=append conv=notrunc of="$file"
So I guess that's another viable choice, as long as your target system
has GNU coreutils.
Reply to: