Programing

Windows cmd stdout 및 stderr을 단일 파일로 리디렉션

crosscheck 2020. 10. 2. 21:33
반응형

Windows cmd stdout 및 stderr을 단일 파일로 리디렉션


DOS 명령 의 모든 출력 (stdout + stderr)을 단일 파일 로 리디렉션하려고 합니다.

C:\>dir 1> a.txt 2> a.txt
The process cannot access the file because it is being used by another process.

가능합니까, 아니면 두 개의 개별 파일로 리디렉션해야합니까?


원하는 :

dir > a.txt 2>&1

구문 2>&12(stderr)을 1(stdout)으로 리디렉션 합니다 . 당신은 또한에 리디렉션하여 메시지를 숨길 수 있습니다 NUL, MSDN에 대한 자세한 설명과 예 .


Anders Lindahl의 대답은 정확하지만 stdout을 파일로 리디렉션하고 stderr도 리디렉션하려는 경우 리디렉션 후에2>&1 지정 되었는지 확인해야합니다 . 그렇지 않으면 작동하지 않습니다.1>

REM *** WARNING: THIS WILL NOT REDIRECT STDERR TO STDOUT ****
dir 2>&1 > a.txt

MSKB의 배경 정보

이 질문에 대한 대답은 정확하지만 실제로 작동 하는 이유 를 설명하는 데 많은 도움 이되지 않으며 구문이 즉시 명확하지 않기 때문에 실제로 무슨 일이 일어나고 있는지 알아보기 위해 빠른 Google을 수행했습니다. 이 정보가 다른 사람들에게 도움이되기를 바라며 여기에 게시하고 있습니다.

에서 촬영 MS 지원 KB 110930 .


MSKB110930에서

명령 프롬프트에서 오류 메시지 리디렉션 : STDERR / STDOUT

요약

'>'기호를 사용하여 애플리케이션에서 출력을 리디렉션 할 때 오류 메시지가 여전히 화면에 인쇄됩니다. 이는 오류 메시지가 종종 표준 출력 스트림 대신 표준 오류 스트림으로 전송되기 때문입니다.

콘솔 (명령 프롬프트) 응용 프로그램 또는 명령의 출력은 종종 두 개의 개별 스트림으로 전송됩니다. 일반 출력은 표준 출력 (STDOUT)으로 전송되고 오류 메시지는 표준 오류 (STDERR)로 전송됩니다. ">"기호를 사용하여 콘솔 출력을 리디렉션하면 STDOUT 만 리디렉션됩니다. STDERR을 리디렉션하려면 리디렉션 기호에 '2>'를 지정해야합니다. 이것은 STDERR 인 두 번째 출력 스트림을 선택합니다.

명령 dir file.xxx( file.xxx존재하지 않음)은 다음 출력을 표시합니다.

Volume in drive F is Candy Cane Volume Serial Number is 34EC-0876

File Not Found

NUL사용하여 출력을 장치로 리디렉션하는 경우 다음과 같이 출력 dir file.xxx > nul의 오류 메시지 부분이 계속 표시됩니다.

File Not Found

오류 메시지를로만 리디렉션하려면 NUL다음 명령을 사용하십시오.

dir file.xxx 2> nul

또는 출력을 한 위치로 리디렉션하고 오류를 다른 위치로 리디렉션 할 수 있습니다.

dir file.xxx > output.msg 2> output.err

"& 1"명령을 사용하여 STDERR의 출력을 STDOUT으로 리디렉션 한 다음 STDOUT의 출력을 파일로 보내 오류 및 표준 출력을 단일 파일로 인쇄 할 수 있습니다.

dir file.xxx 1> output.msg 2>&1

스크립트의 일반 로그 파일에 stdout 및 stderr을 추가하려면 다음을 수행하십시오.

dir >> a.txt 2>&1

맞습니다. 프로세스에 대한 파일 핸들 1은 STDOUT이며 1>또는에 의해 리디렉션됩니다 >(1은 생략 할 수 있습니다. 규칙에 따라 명령 인터프리터 [cmd.exe]가 처리 할 수 ​​있음). 파일 핸들 2는 STDERR이며 2>.

로그 파일을 만드는 데 사용하는 경우 출력을 _uniquely_named_ (예 : 날짜 및 시간 스탬프) 로그 파일로 보내지 않는 한 동일한 프로세스를 두 번 실행하면 리디렉션 된 파일이 ( 대체) 이전 로그 파일.

The >> (for either STDOUT or STDERR) will APPEND not REPLACE the file. So you get a cumulative logfile, showwing the results from all runs of the process - typically more useful.

Happy trails...


I just chopped out the answer as @Anders just posted it, but...

From my Windows help, I searched on redirection (URL ms-its:C:\WINDOWS\Help\ntcmds.chm::/redirection.htm).

You may want to read about >> and | (pipe), too.


There is, however, no guarantee that the output of SDTOUT and STDERR are interweaved line-by-line in timely order, using the POSIX redirect merge syntax.

If an application uses buffered output, it may happen that the text of one stream is inserted in the other at a buffer boundary, which may appear in the middle of a text line.

A dedicated console output logger (I.e. the "StdOut/StdErr Logger" by 'LoRd MuldeR') may be more reliable for such a task.

See: MuldeR's OpenSource Projects


In a batch file (Windows 7 and above) I found this method most reliable

Call :logging >"C:\Temp\NAME_Your_Log_File.txt" 2>&1
:logging
TITLE "Logging Commands"
ECHO "Read this output in your log file"
ECHO ..
Prompt $_
COLOR 0F

Obviously, use whatever commands you want and the output will be directed to the text file. Using this method is reliable HOWEVER there is NO output on the screen.

참고URL : https://stackoverflow.com/questions/1420965/redirect-windows-cmd-stdout-and-stderr-to-a-single-file

반응형