首先,批处理没有像 unix shell 脚本那样捕获多行输出的简单方法。可以使用 FOR /F 逐行构建多行值,但总长度限制在
关于您的问题,如果不使用至少一个临时文件,就无法独立捕获 stdout 和 stderr 。 编辑: 错误,aschipfl found a way。但是,临时文件更快,而且更简单。
这是一个简单的演示,它使用一个文件来捕获标准错误。我假设您最多要捕获一行标准输出和/或标准错误。
for /f "delims=" %%A in ('yourCommand 2^>err.log`) do set "out=%%A"
<err.log set /p "err="
del err.log
这是一个更复杂的示例,它为 stdout 和 stderr 捕获一组行。这里我假设没有任何输出行以: 开头。 FINDSTR 以行号为前缀,后跟 :,FOR /F 解析出用作“数组”索引的行号,以及 : 之后的值。
@echo off
setlocal disableDelayedExpansion
set /a out.cnt=err.cnt=0
for /f "delims=: tokens=1*" %%A in ('yourCommand 2^>err.log ^| findstr /n "^"') do (
set "out.%%A=%%B"
set "out.cnt=%%A"
)
for /f "delims=: tokens=1*" %%A in ('findstr /n "^" err.log') do (
set "err.%%A=%%B"
set "err.cnt=%%A"
)
:: Display the results
setlocal enableDelayedExpansion
echo ** STDOUT **
for /l %%N in (1 1 %out.cnt%) do echo(!out.%%N!
echo ** STDERR **
for /l %%N in (1 1 %err.cnt%) do echo(!err.%%N!
第二次编辑
如果您想正确处理以: 开头的输出,则需要额外的代码。
@echo off
setlocal disableDelayedExpansion
set /a out.cnt=err.cnt=0
for /f "delims=" %%A in ('yourCommand 2^>err.log ^| findstr /n "^"') do for /f "delims=:" %%N in ("%%A") do (
set "ln=%%A"
setlocal enableDelayedExpansion
for /f "delims=" %%B in (^""!ln:*:=!"^") do (
endlocal
set "out.%%N=%%~B"
set "out.cnt=%%N"
)
)
for /f "delims=" %%A in ('findstr /n "^" err.log') do for /f "delims=:" %%N in ("%%A") do (
set "ln=%%A"
setlocal enableDelayedExpansion
for /f "delims=" %%B in (^""!ln:*:=!"^") do (
endlocal
set "err.%%N=%%~B"
set "err.cnt=%%N"
)
)
:: Display the results
setlocal enableDelayedExpansion
echo ** STDOUT **
for /l %%N in (1 1 %out.cnt%) do echo(!out.%%N!
echo ** STDERR **
for /l %%N in (1 1 %err.cnt%) do echo(!err.%%N!
下面我改编了 aschipfl 的第二个代码,该代码避免使用临时文件,以便保留 ! 字符。代码变得越来越丑陋;-)
@echo off
setlocal disableDelayedExpansion
set "STDOUT="
SET "STDERR="
for /f "delims=" %%E in (
'2^>^&1 (for /f "delims=" %%O in ('^
yourCommand^
^^^^^^^| findstr /n /r "^"'^) do @(echo ^^^^^^^|%%O^)^) ^| findstr /n /r "^"'
) do (
set "ln=%%E"
setlocal enableDelayedExpansion
set "ln=x!ln:*:=!"
set "ln=!ln:\=\s!"
if "!ln:~0,2!"=="x|" (
set "ln=!ln:~0,-1!"
for /f "delims=" %%A in (^""!STDOUT!"^") do for /f "delims=" %%B in (^""!ln:*:=!"^") do (
endlocal
set "STDOUT=%%~A%%~B\n"
)
) else (
for /f "delims=" %%A in (^""!STDERR!"^") do for /f "delims=" %%B in (^""!ln:~1!"^") do (
endlocal
set "STDERR=%%~A%%~B\n"
)
)
)
setlocal enableDelayedExpansion
for %%L in (^"^
%= empty line =%
^") do (
if defined STDOUT (
set "STDOUT=!STDOUT:\n=%%~L!"
set "STDOUT=!STDOUT:\s=\!"
set "STDOUT=!STDOUT:~0,-1!"
)
if defined stderr (
set "STDERR=!STDERR:\n=%%~L!"
set "STDERR=!STDERR:\s=\!"
set "STDERR=!STDERR:~0,-1!"
)
)
echo ** STDOUT **
echo(!STDOUT!
echo ** STDERR **
echo(!STDERR!
exit /b
如果结果存储在数组中而不是一对字符串中会更简单一些。
@echo off
setlocal disableDelayedExpansion
set /a out.cnt=err.cnt=1
for /f "delims=" %%E in (
'2^>^&1 (for /f "delims=" %%O in ('^
yourCommand^
^^^^^^^| findstr /n /r "^"'^) do @(echo ^^^^^^^|%%O^)^) ^| findstr /n /r "^"'
) do (
set "ln=%%E"
setlocal enableDelayedExpansion
set "ln=x!ln:*:=!"
if "!ln:~0,2!"=="x|" (
set "ln=!ln:~0,-1!"
for %%N in (!out.cnt!) do for /f "delims=" %%A in (^""!ln:*:=!"^") do (
endlocal
set "out.%%N=%%~A"
set /a out.cnt+=1
)
) else (
for %%N in (!err.cnt!) do for /f "delims=" %%A in (^""!ln:~1!"^") do (
endlocal
set "err.%%N=%%~A"
set /a err.cnt+=1
)
)
)
set /a out.cnt-=1, err.cnt-=1
setlocal enableDelayedExpansion
echo ** STDOUT **
for /l %%N in (1 1 %out.cnt%) do echo(!out.%%N!
echo ** STDERR **
for /l %%N in (1 1 %err.cnt%) do echo(!err.%%N!
exit /b
我知道很多人试图避免使用临时文件,但在这种情况下,我认为它会适得其反。测试表明,当输出非常大时,临时文件可以比使用 FOR /F 循环处理命令的结果快得多。临时文件解决方案要简单得多。所以我肯定会使用临时文件解决方案。
但是找到一个非临时文件解决方案是一个有趣的挑战。感谢 aschipfl 解决了复杂的转义序列。
第三次(最终?)编辑
最后,这是一个消除所有限制的解决方案,除了每个捕获的输出行必须小于大约 8180 字节。
我本可以将整个代码放在一个大循环中,但是转义序列将是一场噩梦。当我将代码分解为更小的子例程时,找出转义序列要简单得多。
我为在底部的 :test 例程中找到的一堆 ECHO 命令捕获了标准输出和标准错误。
::
:: Script to demonstrate how to run one or more commands
:: and capture stdout in one array and stderr in another array,
:: without using a temporary file.
::
:: The command(s) to run should be placed in the :test routine at the bottom.
::
@echo off
setlocal disableDelayedExpansion
if "%~1" equ ":out" goto :out
if "%~1" equ ":err" goto :err
if "%~1" equ ":test" goto :test
set /a out.cnt=err.cnt=0
:: Runs :err, which runs :out, which runs :test
:: stdout is captured in out array, and stderr in err array.
for /f "delims=. tokens=1*" %%A in ('^""%~f0" :err^"') do (
for /f "delims=:" %%N in ("%%B") do (
set "ln=%%B"
setlocal enableDelayedExpansion
for /f "delims=" %%L in (^""!ln:*:=!"^") do (
endlocal
set "%%A.%%N=%%~L"
set "%%A.cnt=%%N"
)
)
)
:: Show results
setlocal enableDelayedExpansion
echo ** STDOUT **
for /l %%N in (1 1 %out.cnt%) do echo(!out.%%N!
echo(
echo ** STDERR **
for /l %%N in (1 1 %err.cnt%) do echo(!err.%%N!
exit /b
:err :: 1) Run the :out code, which swaps stdout with stderr
:: 2) Prefix stream 1 (stderr) output with err.###: where ### = line number
:: 3) Rredirect stream 2 (stdout) to combine with stream 1 (stderr)
2>&1 (for /f "delims=" %%A in ('^""%~f0" :out^|findstr /n "^"^"') do echo err.%%A)
exit /b
:out :: 1) Run the :test code.
:: 2) Prefix stream 1 (stdout) output with out.###: where ### = line number
:: 3) Swap stream 1 (stdout) with stream 2 (stderr)
3>&2 2>&1 1>&3 (for /f "delims=" %%A in ('^""%~f0" :test^|findstr /n "^"^"') do echo out.%%A)
exit /b
:test :: Place the command(s) to run in this routine
echo STDOUT line 1 with empty line following
echo(
>&2 echo STDERR line 1 with empty line following
>&2 echo(
echo STDOUT line 3 with poison characters "(<^&|!%%>)" (^<^^^&^|!%%^>)
>&2 echo STDERR line 3 with poison characters "(<^&|!%%>)" (^<^^^&^|!%%^>)
echo err.4:STDOUT line 4 spoofed as stderr - No problem!
>&2 echo out.4:STDERR line 4 spoofed as stdout - No problem!
echo :STDOUT line 5 leading colon preserved
>&2 echo :STDERR line 5 leading colon preserved
echo ;STDOUT line 6 default EOL of ; not a problem
>&2 echo ;STDERR line 6 default EOL of ; not a problem
exit /b
-- 输出--
** STDOUT **
STDOUT line 1 with empty line following
STDOUT line 3 with poison characters "(<^&|!%>)" (<^&|!%>)
err.4:STDOUT line 4 spoofed as stderr - No problem!
:STDOUT line 5 leading colon preserved
;STDOUT line 6 default EOL of ; not a problem
** STDERR **
STDERR line 1 with empty line following
STDERR line 3 with poison characters "(<^&|!%>)" (<^&|!%>)
out.4:STDERR line 4 spoofed as stdout - No problem!
:STDERR line 5 leading colon preserved
;STDERR line 6 default EOL of ; not a problem
我仍然更喜欢临时文件解决方案;-)