#1
  1. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1106

    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?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,993
    Rep Power
    9397
  4. #3
  5. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1106
    oh! Sorry.
    Using Ubuntu 9, bash shell.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,993
    Rep Power
    9397
    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?
  8. #5
  9. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1106
    Originally Posted by requinix
    are you thinking that everything cat outputs will go there if there's a problem?
    Yes. Isn't it right?
    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.

    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:
    Code:
    (cat < foo) 2> bar
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    You don't really need to redirect stdin into cat ...

    Code:
    cat foo 2> bar
    is a 'cleaner' way of doing it.

    Comments on this post

    • ishnid agrees : Yes, this is the way to do 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
  12. #7
  13. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    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).

    Comments on this post

    • RAJ_55555 agrees : Thanx for your reply
  14. #8
  15. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1106
    @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.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    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.

    Comments on this post

    • RAJ_55555 agrees : Thanx for the confirmation.
    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
  18. #10
  19. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1106
    Thanks a lot both of you. I will be back later with more queries. :-)

IMN logo majestic logo threadwatch logo seochat tools logo