I have a question about permissions.
Distro: Debian GNU/Linux 6.0.4 (squeeze)
So I have a folder with a php script in it. Both folder and file are owned by User1
If I do this:
php script.php
I get a permission denied error. Fair enough; I'm User2, not User1. If I do
sudo php script.php
this works because my user account is setup for that.
So I wrote this script to accept an optional argument to output some stuff, like a "debug" mode, so if I do this:
sudo php script.php debug
It outputs some stuff to CLI. So far so good.
But sometimes the output from the script is too long and I can't scroll up to see the output from the beginning, so I want to be able to redirect the output to a file.
So I have tried the following:
This gives me a permission denied error.
sudo php script.php debug >> results.txt
This works
sudo touch results.txt
sudo chown User1 results.txt
sudo chmod 777 results.txt
sudo php script.php debug >> results.txt
Alternatively, this works
sudo su -
php script.php debug >> results.txt
So what I want to know is... why did the first one give me a permission denied error? I thought the point of the sudo prefix is that I run the command as root, which works for running the php script...but it seems to me that this doesn't extend to executing the redirect? Can someone explain why?
EDIT:
So I found something from another post on here that works:
sudo php script.php debug | sudo tee results.txt
The answer there confirms my suspicion about the redirect still being run under my user and not sudo, but it doesn't explain why...can anybody explain why?
>>file
is a shell feature not a general feature thus is cannot be passed as an argument. The shell forks, redirects I/O and then tries to execute the external command. In your case the redirection fails thus calling sudo is not even tried. SUID rights are relevant only when e.g. files are opened. But most programs don't open /dev/stdout: They simply inherit it from their parent. So it's the parent's access rights which determine the possible redirections. – Hauke Laging Mar 15 '13 at 15:10