【问题标题】:Exit a batch script that called another batch script退出调用另一个批处理脚本的批处理脚本
【发布时间】:2014-12-03 11:31:14
【问题描述】:

我有一个批处理脚本,它调用一系列批处理文件。

有一个错误情况,我需要退出被调用的批处理文件以及父批处理文件。是否可以在子批处理文件中完成此操作?

test.bat

rem Test1.bat will exit with error code.
call test1.bat
rem Want script to stop if test1.bat errors.
call test2.bat

test1.bat

rem Can I get test.bat to terminate from inside test1.bat?
exit /b 1

【问题讨论】:

    标签: windows batch-file cmd call parent


    【解决方案1】:

    您可以使用错误级别。如果被调用的批次系统地使用exit 0通知keep onexit 1要求调用者停止,你可以这样修改调用者:

    rem Test1.bat will exit with error code.
    call test1.bat
    
    rem Want script to stop if test1.bat errors.
    if errorlevel 1 goto fatal
    call test2.bat
    exit 0
    :fatal
    echo Fatal error
    exit 1
    

    【讨论】:

      【解决方案2】:

      您可以通过在子进程中导致致命的语法错误来退出子进程的所有子批处理和父批处理:

      test.bat

      @echo off
      echo before
      call test1.bat
      echo after
      

      test1.bat

      @echo off
      echo in test 1
      call :kill 2>nul
      
      :kill - Kills all batch processing with a fatal syntax error
      () rem fatal syntax error kills batch and all parents
      

      调用 test.bat 会打印“之前”和“在测试 1 中”,但不会打印“之后”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 2014-11-26
        相关资源
        最近更新 更多