On Sun, Nov 21, 2010 at 11:12 PM, Roshan kubunturos@gmail.com wrote:
All,
I'm posting a query to the list after quite a long time.
I'll ask the question right away:
$cat < foo 2>errorfile bash: foo: No such file or directory
Aah, the subtleties of shell plumbing....
First, you need to understand the order in which the command line is parsed, and who is responsible for each bit of I/O.
$cat foo cat: foo: No such file or directory
Here, you see that the cat program throws the error. This is because here cat tries to open the file foo, does not find it and puts it's hands up.
$ cat < foo bash: foo: No such file or directory
In the example above, if you notice the error line, it Says "bash: foo: No such file or directory". Notice "bash:" at the beginning, which says that bash is the one throwing this error. This is because the < operator tells bash that "redirect file to stdin of process". The "bash" shell is responsible for all file I/O here, and any errors with respect to the file I/O will be reported by bash only, not the actual program being executed.
$ cat < foo 2> error bash: foo: No such file or directory
Remember, bash has no idea when the process may be expecting the input, it just has to keep it available on the STDIN fd for whenever the process asks to read it. In order to do that, bash FIRST creates the fd pointing to the file, then creates the fd redirecting STDERR to another file and then finally starts the process with the redirection in place. Now, if the initial creation of the fd does not take place, "bash" thrown the error on it's STDERR fd, not the STDERR fd of "cat"(Actually, the cat process is not actually even started, since the redirection setup failed.) That's why the redirection of the "cat" process STDERR does not capture the errror message.
$ cat foo 2>error $ cat error cat: foo: No such file or directory
Now, this works as expected. I hope that this makes shell redirection a little clearer for you.
Regards R. K. Rajeev
According to the above command, if foo doesn't exist and I redirect
the error stream to a file, it should be stored in the file and not displayed on the terminal. But it still gets displayed!
At a point, I thought this is a trick question and thought that possibly, foo exists and its contents are what is shown on the display. (But that isn't really the case) (because the errorfile doesn't get created at all - not in the above one and not according to the answer I've given - i.e. being a trick question).
I think, though, there's something that I do not know about the input redirection ( i.e. left chevron '<') and hence I'm not able to answer this query. (because, possibly, the error redirection 2> command is ignored completely / not executed at all).
Can anybody point me to a link that explains why this happens in Bash ? (Or perhaps, even in Korn shell?) (or if anybody can put a brief explanation of this?)
-- Roshan Baladhanvi -- http://mm.glug-bom.org/mailman/listinfo/linuxers