Discuss Doesn't get redirected to std error in the Linux Help forum on Dev Shed. Doesn't get redirected to std error Linux Help forum discussing topics including usage, troubleshooting, modules, and distributions. Linux is an open source OS, based on UNIX.
Posts: 749
Time spent in forums: 1 Week 6 Days 15 h 50 m 39 sec
Reputation Power: 1105
Doesn't get redirected to std error
I am not really a Linux or unix user, so apologies if the question seems too easy to be asked on the forum, but I tried using the search engines and failed.
Code:
cat < foo 2> bar
why doesn't this get redirected to stderror on error conditions?
Posts: 12,701
Time spent in forums: 5 Months 1 Week 4 Days 5 h 23 m 54 sec
Reputation Power: 8969
By "redirected to stderror" (it's called just stderr, by the way) are you thinking that everything cat outputs will go there if there's a problem? Because that's not how it works: cat can choose whether to output to stdout or stderr and, as per general Unix convention, it'll output normal stuff to stdout and warning messages to stderr.
1. Does foo exist? Is it a file, directory, or something else?
2. What output do you get? What's in bar?
3. What output do you expect? What do you expect to see in bar?
Posts: 749
Time spent in forums: 1 Week 6 Days 15 h 50 m 39 sec
Reputation Power: 1105
Quote:
Originally Posted by requinix
are you thinking that everything cat outputs will go there if there's a problem?
Yes. Isn't it right?
Quote:
Originally Posted by requinix
it'll output normal stuff to stdout and warning messages to stderr.
you are also saying the same thing, or may be I didn't understand. please clarify.
Quote:
Originally Posted by requinix
Does foo exist? Is it a file, directory, or something else?
No. If foo exists, then as expected it prints the contents of the file. But if it doesn't the error message is also printed on the terminal. I expect it to be redirected to the file 'bar', which also doesn't exist, but I think it is created automatically.
I tried a couple of things, and it behaves as I expected it to by using this:
Posts: 2,108
Time spent in forums: 1 Month 1 Week 1 Day 4 h 41 m
Reputation Power: 1485
You don't really need to redirect stdin into cat ...
Code:
cat foo 2> bar
is a 'cleaner' way of doing it.
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc
Posts: 6,894
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 36 m 34 sec
Reputation Power: 3885
Quote:
Originally Posted by RAJ_55555
No. If foo exists, then as expected it prints the contents of the file. But if it doesn't the error message is also printed on the terminal. I expect it to be redirected to the file 'bar'...
The way you have it set up, it's anything that "cat" prints to stderr that goes to the file "bar". My understanding is that because you've used a '<' to read the file, it's bash (rather than "cat") that's reading the file for you, then passing its contents into "cat". As the file doesn't exist, its output is printed to stderr (which is your terminal screen since you haven't redirected it).
Posts: 749
Time spent in forums: 1 Week 6 Days 15 h 50 m 39 sec
Reputation Power: 1105
@SimonJM
I know that I don't need to redirect stdin into cat. But I was learning to use cat with foo, without informing cat of the existence of the file. Thus the redirection. Thanks :-p
@ishnid
I thought somewhat similar, but wasn't(isn't) exactly clear to me. I understand that bash reads the file foo and if the file doesn't exist, its output goes to stderr.
Now, what you are saying is that the stderr for cat is the file 'bar', but for the shell, its the Terminal. Am I right? I think it makes sense.
Posts: 2,108
Time spent in forums: 1 Month 1 Week 1 Day 4 h 41 m
Reputation Power: 1485
Yes, that is pretty much it ... each command, process, etc. runs under it's own steam in *nix systems - unless you 'bundle' them together using brackets. Thus your cat < foo 2> bar is, in effect, 2 things - first the 'silent' copying of foo into stdin by the shell, then the cat command processing that input. If the file foo does not exist that is handled by the shell, whereas the redirection of stderr used is aimed at the cat command.
When you used (cat < foo) you bundled the two together so the stderr redirection worked on the error output from both.