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?
Output/Redirection Question 1> and 2>
Moderator: 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:
Some examples:
And this won't not work:
DosItHelp?
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 GOOD
command 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?
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Re:
DosItHelp wrote:command >file.txt 2>&1 GOOD
command 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
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Re: Output/Redirection Question 1> and 2>
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 . . .
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>
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
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