退出调用另一个批处理脚本的批处理脚本

3

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

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

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
2个回答

3
通过使用errorlevel,您可以实现这一功能。如果被调用的批处理程序系统性地使用exit 0来通知继续运行,并使用exit 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

0

您可以通过在子进程中引发致命语法错误来退出所有子进程和父进程:

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 命令会输出 "before" 和 "in test 1",但不会输出 "after"。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接