批处理文件检查自己的输出?

Han*_*nna 4 command-line batch-file maven

我想知道是否有可能让批处理文件检查字符串.

我正在使用批处理文件来运行Maven命令,我想通过在脚本末尾搜索"FAILURE"字符串来检查是否有任何失败

我知道你可以FIND在其他文件中,但是你可以检查它本身的当前输出,还是将批处理文件输出保存为文本然后搜索它的最佳解决方案?

作为一个例子,如果我有一个批处理文件,echo Hello World它会打印Hello World,然后我想搜索输出Hello并告诉我它找到了字符串Hello.

dbe*_*ham 8

我喜欢vane的想法,对mvn提供的返回代码采取行动,但我不喜欢使用ERRORLEVEL,而是喜欢使用||运算符.||如果先前的命令失败,则仅执行后面的命令.

::initialize error flag to undefined
set "mvnErr="

::run your Maven command and define the error flag if there was an error
call mvn {your arguments here} || set mvnErr=1

::You can now take action if there was an error
if defined mvnErr echo there was a Maven error
Run Code Online (Sandbox Code Playgroud)


van*_*ane 6

您可以通过检查errorlevel每个Maven命令后执行此操作.例如

@ECHO OFF

set hasErrors=0

REM execute maven command here
if not errorlevel 0 set hasErrors=1

REM more batch command and similar checking ...

if %hasErrors%==1 (
    echo print your error info or do whatever
)
Run Code Online (Sandbox Code Playgroud)