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
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
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
On Mon, Nov 22, 2010 at 8:37 AM, rajeevrkv wrote:
Aah, the subtleties of shell plumbing....
The way that plumbing works is fabulous. (I had read about this to an extent, but there were more details as you've explained below)
$ cat < foo bash: foo: No such file or directory
Yes, I failed to notice the difference initially, though I knew that '<' will cause an input to be redirected from the file to STDIN of process. (cat)
$ 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.
Thanks for the detailed explanation I did understand what you said. (Bash expands the meta-characters first and then processes the command).
So effectively, whenever, we are redirecting to STDOUT or STDERR, the command (process) that is executed, its output or error is put into a file. Anytime, the input for bash fails (via file through < operator), the subsequent STDOUT and STDERR are reverted to usual (the terminal) [even if appeared to be changed in the actual command].
I hope that this makes shell redirection a little clearer for you.
It does. It gives me more insight into the way Bash works. If there's a book / link, which provides such details with examples, I'll be able to understand bash better than I currently do.
Thankyou very much. You've been a patient guy in explaining all this. (I remember / know you when I met you at one of the colleges explaining students about Fedora Linux) :)
-- Roshan Baladhanvi
2010/11/22 Roshan kubunturos@gmail.com:
So effectively, whenever, we are redirecting to STDOUT or STDERR, the command (process) that is executed, its output or error is put into a file. Anytime, the input for bash fails (via file through < operator), the subsequent STDOUT and STDERR are reverted to usual (the terminal) [even if appeared to be changed in the actual command].
Bash will process the command line left-to-right for redirections. So, you will see different behaviour depending on whether you run:
cat < foo 2> err or cat 2> err < foo
That also explains the differing behaviours of:
ls -ld /tmp /nonexistent > /tmp/output 2>&1 and ls -ld /tmp /nonexistent 2>&1 > /tmp/output
Also: the '<' redirection for input is for the command that bash runs, not for bash itself (but see the documentation of the "exec" bash builtin).
Binand
Roshan wrote:
cat< foo 2>errorfile
On my system it does redirect to the errorfile. Please also see http://books.google.co.in/books?id=qX3CCAnjSPwC&pg=PA158&lpg=PA158&a...<+foo+2>errorfile&source=bl&ots=o-BohSIkJP&sig=Hv4g5xB1oN803-vaH1CANc_h_Jc&hl=en&ei=KunpTNaGC9TCcff7rIcL&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBUQ6AEwAA#v=onepage&q=cat < foo 2>errorfile&f=false
http://www.beforever.com/bashtut.htm#output
Hope this helps.
sadhu
2010/11/22 Nachiketa Sadhu sadhu@iitb.ac.in:
Roshan wrote:
cat< foo 2>errorfile
On my system it does redirect to the errorfile.
Strange. Which system would that be? Can you please show us a pastebin?
Binand
Binand Sethumadhavan wrote:
2010/11/22 Nachiketa Sadhusadhu@iitb.ac.in:
Roshan wrote:
cat< foo 2>errorfile
On my system it does redirect to the errorfile.
Strange. Which system would that be? Can you please show us a pastebin?
Binand
Extremely sorry, I am at fault. I tried a different command
cat foo 2>errorfile
sadhu