簡體   English   中英

退出調用另一個批處理腳本的批處理腳本

[英]Exit a batch script that called another batch script

我有一個批處理腳本,它調用一系列批處理文件。

有一個錯誤情況,我需要退出被調用的批處理文件以及父批處理文件。 是否可以在子批處理文件中完成此操作?

測試.bat

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

測試1.bat

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

您可以通過使用錯誤級別。 如果被調用的批次系統地使用exit 0來通知keep on並使用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

您可以通過在子進程中導致致命的語法錯誤來退出子進程的所有子進程和父進程:

測試.bat

@echo off
echo before
call test1.bat
echo after

測試1.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”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM