Page 1 of 1
Output/Redirection Question 1> and 2>
Posted: 17 Dec 2009 13:30
by squadjot
Hi, i have one more question =)
I use the > syntax to output the console to a file.
The thing is, in some cases i have to use 2> to catch errors.
Does anybody know how i catch BOTH non-error -and error-messages?
Posted: 17 Dec 2009 23:48
by DosItHelp
squadjot,
redirect stderr into stdout in other words:
redirect 2 into &1 like this:
command >file.txt 2>&1
Note: the order you put the redirection is important:
command >file.txt 2>&1 GOODcommand 2>&1 >file.txt BAD
Some examples:
Code: Select all
C:\>(echo.good & set /A)
good
The syntax of the command is incorrect.
C:\>(echo.good & set /A) >NUL
The syntax of the command is incorrect.
C:\>(echo.good & set /A) 2>NUL
good
C:\>(echo.good & set /A) >NUL 2>&1
C:\>(echo.good & set /A) >aa.txt 2>&1
C:\>type aa.txt
good
The syntax of the command is incorrect.
And this won't not work:
Code: Select all
C:\>(echo.good & set /A) 2>&1 >aa.txt
The syntax of the command is incorrect.
C:\>(echo.good & set /A) 2>aa.txt 1>aa.txt
The process cannot access the file because it is being used by another process.
DosItHelp?
Posted: 18 Dec 2009 15:48
by avery_larry
I'd like to note that the >nul is a special case, where you CAN do this:
(echo.good & set /A) >nul 2>nul
But if you're redirecting to a file, you'll get the "file in use" error, and as such need the >&1 notation as noted above.
Same rules for append >>a.txt 2>>&1
Re:
Posted: 22 Jan 2010 05:39
by alan_b
DosItHelp wrote:command >file.txt 2>&1 GOODcommand 2>&1 >file.txt BAD
BAD is worse than GOOD, but there is BETTER ! !
I found that sometimes the two streams collided and file.txt missed the data,
and instead I got an error.
I found it was necessary to redirect one stream to the log file,
and the other stream to a temporary file,
after which the temporary file was appended to the log file.
Regards
Alan
Re: Output/Redirection Question 1> and 2>
Posted: 22 Jan 2010 10:54
by avery_larry
I've never seen an error with 1>file.txt 2>&1
Problem with your method -- the lines will be out of order. But if that's the only way to catch everything . . .
Re: Output/Redirection Question 1> and 2>
Posted: 22 Jan 2010 13:11
by alan_b
There is no problem with the message order.
Normally a command may give only a response via either STDOUT or STDERR,
in which case there is no possibility of the wrong order.
Under some conditions some commands may result in a response from both,
in which case the order in which they appear is under control of my script.
I accept that my script will always present my chosen order instead of the order in which they would have appeared on the screen had there been no redirection, but I prefer consistent order instead of the random chaos of sundry race hazards.
N.B. I cannot remember the precise details of how STDOUT and STDERR occur "simultaneously",
but I think it involved piping output from one command through another, such as FIND.EXE.
Alan