1833006 Members
3105 Online
110048 Solutions
New Discussion

Re: redirection problem

 
amit singh_3
Advisor

redirection problem

Does any one tell me what is the diffrence,
between 1>#ls >file 2>&1 and
2>#ls 2>&1 >file
why the 2nd cmd not redirecting the std error
to the file?
If any one knows anything pls let me know.

Amit Singh
5 REPLIES 5
Mark Grant
Honored Contributor

Re: redirection problem

Well, you're right! My guess is that the shell parses from left to right so when it sees the "2>&1" it sets up standard error to whatever standard output is. Standard output hasn't actually been re-directed yet so it is to your terminal. Then the shell sees the ">file" and re-directs standard output to the file as you wanted. However, standard error has already been re-directed to your screen.

"kjk 2>&1 >file 2>&1" although stupid achieves what you would expect if the above paragraph is true.

I think the point is that "2>&1" doesn't mean "glue" standard error to standard output but means direct standard error to wherever standard output happens to be right now.
Never preceed any demonstration with anything more predictive than "watch this"
ranganath ramachandra
Esteemed Contributor

Re: redirection problem

i dont know why ... but there are some things about this that i found. since i dont know your exact problem, guessed this may help you in whaterver it is you are trying.

enclose the command within parenthesis or braces and you dont have the problem:

ls nosuchfile 2>&1 >log #doesnt work
(ls nosuchfile 2>&1) > log # works
{ ls nosuchfile 2>&1 \
} > log #works

also if you put the offending line within a script file and run it (redirecting stdout) it still works.
 
--
ranga
hp-ux 11i v3[i work for hpe]

Accept or Kudo

amit singh_3
Advisor

Re: redirection problem

hey Mark,
Ur explanation is good.
do U have any document explaning the fact?
or do U have any info from where I will get
the HP internals book.

thanks..

Amit Singh
Mark Grant
Honored Contributor

Re: redirection problem

I don't have any documentation, just far more experience than I care to admit. However, I think careful reading of the sh-posix man page will probably indicate the reasoning here.
Never preceed any demonstration with anything more predictive than "watch this"
Jeroen Peereboom
Honored Contributor

Re: redirection problem

Amit,

check the man-page from sh-posix.
Quote:

If any of the above redirections is preceded by a digit (0 to 9), the file descriptor used is the one specified by
the digit, instead of the default 0 (standard input) or 1 (standard output). For example:
2>&1
means open file descriptor 2 for writing as a duplicate of file descriptor 1. Output directed to file descriptor
2 is written in the same location as output to file descriptor 1.
Order is significant in redirection. The shell evaluates each redirection in terms of the (file descriptor, file)
assignment at the time of evaluation. For example:
1>fname 2>&1
first assigns file descriptor 1 to file fname. It then assigns file descriptor 2 to the file assigned to file
descriptor 1 (that is, fname).
If the order of redirection is reversed, as in
2>&1 1>fname
file descriptor 2 is assigned to the file assigned to file descriptor 1 (probably the terminal) and then file
descriptor 1 is assigned to file fname.

JP.

(Glad to help, bu if this answer or any of the above help you, would you assign some points?)