如何在Windows批处理编程中获取管道中命令的错误级别?

时间:2021-08-11 02:08:19

Batch files return the error code of the last command by default.

批处理文件默认返回最后一个命令的错误代码。

Is it somehow possible to return the error code of a former command. Most notably, is it possible to return the error code of a command in a pipe?

是否有可能返回前一个命令的错误代码。最值得注意的是,是否可以在管道中返回命令的错误代码?

For example, this one-line batch script

例如,这个单行批处理脚本

foo.exe

returns the error code of foo. But this one:

返回foo的错误代码。但是这一个:

foo.exe | tee output.txt

always returns the exit code of tee, which is zero.

始终返回tee的退出代码,该代码为零。

6 个解决方案

#1


I had a similar problem and settled on the following solution as I did not need to detect the exact error code just success or failure.

我有一个类似的问题,并解决了以下解决方案,因为我不需要检测确切的错误代码只是成功或失败。

echo > .failed.tmp    

( foo.exe && del .failed.tmp ) | tee foo.log

if exist .failed.tmp (
    del .failed.tmp
    exit /b 1
) else (
    exit /b 0
)

#2


The %ERRORLEVEL% variable doesn't get updated before the piped command is run; you have to use a wrapper as advised in the other answers.

在运行管道命令之前,%ERRORLEVEL%变量不会更新;您必须按照其他答案中的建议使用包装器。

However, you can use "IF ERRORLEVEL #". For example:

但是,您可以使用“IF ERRORLEVEL#”。例如:

(
type filename
@REM Use an existing (or not) filename to test each branch
IF ERRORLEVEL 1 (echo ERROR) ELSE (echo OKAY)
) > logfile.txt

The ECHO will only run if an error was returned; however %ERRORLEVEL% seems inconsistent.

只有在返回错误时才会运行ECHO;但%ERRORLEVEL%似乎不一致。

Edit: Changed example to be runnable as-is.

编辑:将示例更改为可按原样运行。

#3


One workaround is to make an indirection through a file.

一种解决方法是通过文件进行间接寻址。

Like this

foo.exe > tmp.txt
set FOOERR=%ERRORLEVEL%
cat tmp.txt
exit %FOOERR%

#4


After about one day of digging, I found a way to do that:

经过大约一天的挖掘,我找到了一种方法:

set error_=0
9>&1 1>&2 2>&9 (for /f "delims=" %%i in ('9^>^&1 1^>^&2 2^>^&9 ^(^(^(2^>^&1 call "%homeDir%%1"^) ^|^| ^(1^>^&2 2^>nul echo FAILED^)^) ^| 2^>nul "%homeDir%mtee" /T /+ "%homeDir%logs\%date_%_%1.log"^)') do (set error_=1))

exit /b %error_%

In the example above "%homeDir%%1" is being executed and its output is piped to "%homeDir%mtee". This line detects failures (I'd suggest you to draw a diagram of batch contexts and their stdin/stdout/stderr assignments in order to understand what it does :-) ). I did not find a good way to extract the actual errorlevel. The best thing I got was to replace the 'echo' command with some batch script call 'call rc.bat' which look like:

在上面的示例中,“%homeDir %% 1”正在执行,其输出通过管道传送到“%homeDir%mtee”。这行检测失败(我建议你绘制批处理上下文及其stdin / stdout / stderr赋值的图表,以便了解它的作用:-))。我没有找到提取实际错误级别的好方法。我得到的最好的事情是用一些批处理脚本调用'call rc.bat'替换'echo'命令,它看起来像:

@echo %errorlevel%

and then replace 'set error_=1' with 'set error_=%%i'.

然后用'set error _ = %% i'替换'set error_ = 1'。

But the problem is that this call may fail too, and it is not easy to detect that. Still, it is much better than nothing -- I did not find any solution for that on the Internet.

但问题是这个调用可能也会失败,并且检测它并不容易。不过,它总比没有好 - 我没有在互联网上找到任何解决方案。

#5


You can solve the problem by creating a wrapper around your command file:

您可以通过在命令文件周围创建包装器来解决问题:

rem wrapper for command file, wrapper.cmd

call foo.exe

echo %errorlevel%

if errorlevel 1 goto...

Then append tee to the wrapper:

然后将三通附加到包装器:

wrapper.cmd | tee result.log

Of course this does not exactly the same, e.g. if you want to log in several files in the wrapped file, it is not possible, but in my case it solved the problem.

当然,这并不完全相同,例如如果你想在包装文件中登录几个文件,那是不可能的,但在我的情况下它解决了这个问题。

#6


To call tee for entry bat-file, not for single command, and use errorlevel freely, I use trick like this:

要为入口bat文件调用tee,而不是单个命令,并*使用errorlevel,我使用这样的技巧:

if "%1" == "body" goto :body
call %0 body | tee log.txt
goto :eof
:body

set nls_lang=american_america
set HomePath=%~dp0

sqlplus "usr/pwd@tnsname" "@%HomePath%script.sql" 
if errorlevel 1 goto dberror

rem Here I can do something which is dependent on correct finish of script.sql    

:dberror

echo script.sqlerror failed

it separates using tee from calling any commands inside batch.

它使用tee来分离批处理中的任何命令。

#1


I had a similar problem and settled on the following solution as I did not need to detect the exact error code just success or failure.

我有一个类似的问题,并解决了以下解决方案,因为我不需要检测确切的错误代码只是成功或失败。

echo > .failed.tmp    

( foo.exe && del .failed.tmp ) | tee foo.log

if exist .failed.tmp (
    del .failed.tmp
    exit /b 1
) else (
    exit /b 0
)

#2


The %ERRORLEVEL% variable doesn't get updated before the piped command is run; you have to use a wrapper as advised in the other answers.

在运行管道命令之前,%ERRORLEVEL%变量不会更新;您必须按照其他答案中的建议使用包装器。

However, you can use "IF ERRORLEVEL #". For example:

但是,您可以使用“IF ERRORLEVEL#”。例如:

(
type filename
@REM Use an existing (or not) filename to test each branch
IF ERRORLEVEL 1 (echo ERROR) ELSE (echo OKAY)
) > logfile.txt

The ECHO will only run if an error was returned; however %ERRORLEVEL% seems inconsistent.

只有在返回错误时才会运行ECHO;但%ERRORLEVEL%似乎不一致。

Edit: Changed example to be runnable as-is.

编辑:将示例更改为可按原样运行。

#3


One workaround is to make an indirection through a file.

一种解决方法是通过文件进行间接寻址。

Like this

foo.exe > tmp.txt
set FOOERR=%ERRORLEVEL%
cat tmp.txt
exit %FOOERR%

#4


After about one day of digging, I found a way to do that:

经过大约一天的挖掘,我找到了一种方法:

set error_=0
9>&1 1>&2 2>&9 (for /f "delims=" %%i in ('9^>^&1 1^>^&2 2^>^&9 ^(^(^(2^>^&1 call "%homeDir%%1"^) ^|^| ^(1^>^&2 2^>nul echo FAILED^)^) ^| 2^>nul "%homeDir%mtee" /T /+ "%homeDir%logs\%date_%_%1.log"^)') do (set error_=1))

exit /b %error_%

In the example above "%homeDir%%1" is being executed and its output is piped to "%homeDir%mtee". This line detects failures (I'd suggest you to draw a diagram of batch contexts and their stdin/stdout/stderr assignments in order to understand what it does :-) ). I did not find a good way to extract the actual errorlevel. The best thing I got was to replace the 'echo' command with some batch script call 'call rc.bat' which look like:

在上面的示例中,“%homeDir %% 1”正在执行,其输出通过管道传送到“%homeDir%mtee”。这行检测失败(我建议你绘制批处理上下文及其stdin / stdout / stderr赋值的图表,以便了解它的作用:-))。我没有找到提取实际错误级别的好方法。我得到的最好的事情是用一些批处理脚本调用'call rc.bat'替换'echo'命令,它看起来像:

@echo %errorlevel%

and then replace 'set error_=1' with 'set error_=%%i'.

然后用'set error _ = %% i'替换'set error_ = 1'。

But the problem is that this call may fail too, and it is not easy to detect that. Still, it is much better than nothing -- I did not find any solution for that on the Internet.

但问题是这个调用可能也会失败,并且检测它并不容易。不过,它总比没有好 - 我没有在互联网上找到任何解决方案。

#5


You can solve the problem by creating a wrapper around your command file:

您可以通过在命令文件周围创建包装器来解决问题:

rem wrapper for command file, wrapper.cmd

call foo.exe

echo %errorlevel%

if errorlevel 1 goto...

Then append tee to the wrapper:

然后将三通附加到包装器:

wrapper.cmd | tee result.log

Of course this does not exactly the same, e.g. if you want to log in several files in the wrapped file, it is not possible, but in my case it solved the problem.

当然,这并不完全相同,例如如果你想在包装文件中登录几个文件,那是不可能的,但在我的情况下它解决了这个问题。

#6


To call tee for entry bat-file, not for single command, and use errorlevel freely, I use trick like this:

要为入口bat文件调用tee,而不是单个命令,并*使用errorlevel,我使用这样的技巧:

if "%1" == "body" goto :body
call %0 body | tee log.txt
goto :eof
:body

set nls_lang=american_america
set HomePath=%~dp0

sqlplus "usr/pwd@tnsname" "@%HomePath%script.sql" 
if errorlevel 1 goto dberror

rem Here I can do something which is dependent on correct finish of script.sql    

:dberror

echo script.sqlerror failed

it separates using tee from calling any commands inside batch.

它使用tee来分离批处理中的任何命令。