我有一个批处理脚本,它调用批处理文件序列。
有一种错误情况,需要退出被调用的批处理文件以及父批处理文件。是否有可能在子批处理文件中完成此操作?
test.bat
rem Test1.bat will exit with error code.
call test1.bat
rem Want script to stop if test1.bat errors.
call test2.battest1.bat
rem Can I get test.bat to terminate from inside test1.bat?
exit /b 1发布于 2014-10-08 18:24:18
你可以,通过使用错误级别。如果被调用的批处理系统地使用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发布于 2020-05-21 20:25:51
通过在子进程中导致致命语法错误,可以从子进程中退出所有子批处理和父批处理进程:
test.bat
@echo off
echo before
call test1.bat
echo aftertest1.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中”和“在测试1中”,而不是“之后”。
https://stackoverflow.com/questions/26263111
复制相似问题