批处理文件默认返回最后一条命令的错误代码。
是否有可能以某种方式返回先前命令的错误代码。最值得注意的是,是否可以在管道中返回命令的错误代码?
例如这个一行的批处理脚本
foo.exe
返回foo 的错误代码。但是这个:
foo.exe | tee output.txt
始终返回 tee 的退出代码,即零。
最佳答案
我遇到了类似的问题并选择了以下解决方案,因为我不需要检测准确的错误代码,只需检测成功或失败。
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
)
关于windows - 如何在 Windows 批处理程序中获取管道中命令的错误级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/877639/