[ale] Redirection using sudo...?
Michael B. Trausch
fd0man at gmail.com
Tue Jun 13 16:28:03 EDT 2006
On Tue, June 13 2006 15:15, Geoffrey wrote:
> Michael B. Trausch wrote:
> > Okay, so I know that the shell itself implements redirection. This is
> > not a problem for me, as I use various forms of redirection and piping
> > and such all the time. However, I have wondered if there is a way to
> > change how that works.
> >
> > Sometimes, I want to change something in /proc or /sys. Doing
> > something like:
> >
> > $ sudo echo "newvalue" > /sys/something
>
> Have you tried quoting the command?
>
> sudo 'echo "newvalue" > /sys/something'
>
That doesn't quite work because it doesn't start a subshell. I was looking
at the problem from the wrong angle, due to an errant assumption on my
part. 'sudo' handles the execution of the program itself, instead of using
a subshell to do it in. Thus:
fd0man at fd0man-laptop:~$ sudo 'echo fi > /fi'
sudo: echo fi > /fi: command not found
fd0man at fd0man-laptop:~$
... does not work because it then interprets the entire string as a command
that it has to execute. You *could* have a file named that on the
filesystem and execute it, but it would terribly bad form.
Likewise, 'sudo -i' doesn't work in this way, either:
fd0man at fd0man-laptop:~$ sudo -i 'echo fi > /fi'
-bash: echo fi > /fi: No such file or directory
fd0man at fd0man-laptop:~$
However, the one way that does work is:
fd0man at fd0man-laptop:~$ sudo sh -c 'echo fi > /fi'
fd0man at fd0man-laptop:~$ cat /fi
fi
... because you are then passing the string to bash to interpret as a
command. Makes perfect sense now that I have seen somebody else do it --
it made me hit myself on the head. :)
- Mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: not available
More information about the Ale
mailing list