批量设置输出和命令的错误以分隔变量

时间:2015-12-28 02:28:10

标签: batch-file windows-7 batch-processing io-redirection

在Windows 7批处理(cmd.exe命令行)中,我试图将命令的标准输出(stdout)和标准错误(stderr)重定向到单独的变量(因此第一个变量设置为输出,并且第二个变量设置为错误(如果有))而不使用任何临时文件。我尝试过并试过没有成功。

那么,设置命令的输出和错误以分离变量的工作方式是什么?

3 个答案:

答案 0 :(得分:3)

您可以选择两个嵌套for /F循环,其中内部循环捕获标准输出,外部循环捕获重定向错误。由于内部实例是一个新的cmd进程,因此捕获的文本不能仅仅分配给变量,因为它将在执行完成后丢失。相反,我在|的每一行之前,只是将它回显到标准输出。外循环检测前导|并相应地分隔行:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "STDOUT="
set "STDERR="
(set LF=^
%=empty line=%
)
for /F "delims=" %%E in ('
    2^>^&1 ^(^
        for /F "delims=" %%O in ^('^
            command_line^
        '^) do @^(^
            echo ^^^|%%O^
        ^)^
    ^)
') do (
    set "LINE=%%E"
    if "!LINE:~,1!"=="|" (
        set "STDOUT=!STDOUT!!LINE:~1!!LF!"
    ) else (
        set "STDERR=!STDERR!!LINE!!LF!"
    )
)
echo ** STDOUT **!LF!!STDOUT!
echo ** STDERR **!LF!!STDERR!
endlocal
exit /B

以下限制适用于代码:

  • 忽略空行;
  • 以分号;开头的行将被忽略;
  • 感叹号!丢失,因为启用了延迟的环境变量扩展;
  • 以管道符|开头的行可能被错误地分配;
  • 数据的总大小不得超过8190字节;

所有这些限制都适用于标准输出和标准错误。

编辑:

以下是上述代码的改进版本。有关以分号;开头的空行和行的问题已得到解决,其他限制仍然存在:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "STDOUT="
set "STDERR="
(set LF=^
%=empty line=%
)
for /F "delims=" %%E in ('
    2^>^&1 ^(^
        for /F "delims=" %%O in ^('^
            command_line ^^^^^^^| findstr /N /R "^"^
        '^) do @^(^
            echo ^^^^^^^|%%O^
        ^)^
    ^) ^| findstr /N /R "^"
') do (
    set "LINE=%%E"
    set "LINE=!LINE:*:=!"
    if "!LINE:~,1!"=="|" (
        set "STDOUT=!STDOUT!!LINE:*:=!!LF!"
    ) else (
        set "STDERR=!STDERR!!LINE!!LF!"
    )
)
echo ** STDOUT **!LF!!STDOUT!
echo ** STDERR **!LF!!STDERR!
endlocal
exit /B

findstr命令用于在行号加:的每一行之前,因此for /F没有任何行显示为空;当然,此前缀将在以后删除。此更改还隐式解决了;问题。

由于findstr的嵌套管道,只要实际需要管道功能,就需要多次转义才能隐藏|字符。

答案 1 :(得分:2)

首先,批处理没有一个简单的方法来捕获多行输出,如unix shell脚本。您可以使用FOR / F逐行构建多行值,但总长度限制为< 8191个字节,语法很笨拙。或者您可以使用FOR / F在模拟的变量数组中捕获多行。

关于你的问题,没有使用至少一个临时文件就无法独立捕获stdout和stderr。 编辑: 错误,aschipfl found a way。但是,临时文件更快,更简单。

这是一个使用文件捕获stderr的简单演示。我假设您想要捕获最多一行stdout和/或stderr。

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解析出要用作&#34;数组的行号。 index,以及:之后的值。

@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制定复杂的逃逸序列。

第3名(最终?)编辑

最后,这是一个消除所有限制的解决方案,除了每个捕获的输出行必须小于大约8180字节。

我本可以将整个代码放在一个大循环中,但转换序列本来就是一场噩梦。当我将代码分解为更小的子程序时,弄清楚转义序列会更简单。

我捕获了stdout和stderr,找到了底部的: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

仍然更喜欢临时文件解决方案; - )

答案 2 :(得分:1)

只要发送到stdout的行以行号分隔的行号开头,此解决方案就能正常工作。

@echo off
setlocal EnableDelayedExpansion

set /A out=0, err=1
for /F "tokens=1* delims=:" %%a in ('(theCommand 1^>^&2 2^>^&3 ^| findstr /N "^"^) 2^>^&1') do (
   if "%%a" equ "!err!" (
      set "stderr[!err!]=%%b"
      set /A err+=1
   ) else (
      set /A out+=1
      if "%%b" equ "" (
         set "stdout[!out!]=%%a"
      ) else (
         set "stdout[!out!]=%%a:%%b"
      )
   )
)
set /A err-=1

echo Lines sent to Stdout:
for /L %%i in (1,1,%out%) do echo !stdout[%%i]!
echo/
echo Lines sent to Stderr:
for /L %%i in (1,1,%err%) do echo !stderr[%%i]!

例如,如果命令是这个.bat文件:

@echo off
echo Line one to stdout
echo Line one to stderr >&2
echo Line two to stderr >&2
echo Line two to stdout

...然后这是输出:

Lines sent to Stdout:
Line one to stdout
Line two to stdout

Lines sent to Stderr:
Line one to stderr
Line two to stderr