从一个函数内部退出批处理脚本 [英] Exit batch script from inside a function

查看:152
本文介绍了从一个函数内部退出批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的批处理文件有问题。
它通过做这样的事情会自动建立几个程序:

I have a problem with my batch file. It builds several programs automatically by doing something like this:


  • 设置一些编译标志

  • 运行GMAKE所有

  • 称之为检查错误级别的功能,如果错误级别1,退出

因此​​,它看起来是这样的:

So it looks like this:

set FLAG=1
...
gmake all
call :interactive_check
set OTHERFLAG=1
...
gmake all
call :interactive_check

有6或7这些(以及它可能会增长)。所以我做了一个函数在每一步检查错误级别,而不是复制/粘贴它。
问题是这样的:错误检查是通过函数取得:

There's 6 or 7 of these (and it might grow). So I made a function to check errorlevel instead of copy/pasting it at every step. The problem is this: the error checking is made through a function:

:interactive_check
if errorlevel 1 (
echo.
echo /!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\
echo Error in compilation process... exiting
echo /!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\/!\
echo.
cd %root_dir%
exit /B 1
) ELSE (
echo.Continuing to next step
)
goto:eof

现在,运行它时,在退出/ B 1 简单地退出功能,而不是批处理文件

Now, when running it, the exit /B 1 simply exits the function, but not the batch file.

你知道如何退出完整的批处理文件,而无需复制/粘贴我的如果错误级别1 ..步步为营?

Do you know how to exit the complete batch file without having to copy/paste my "if errorlevel 1.." at every step?

推荐答案

使用致命的语法错误,如杰布演示,确实杀死所有的批量处理,但它也有一个讨厌的副作用 - 环境的变化SETLOCAL之后是preserved,尽管它们应该当批处理结束要经由隐ENDLOCAL丢弃。见批次结束后我DosTips后 SETLOCAL继续!了解更多信息。

Using a fatal syntax error, as jeb demonstrates, does kill all batch processing, but it also has a nasty side effect - environment changes after SETLOCAL are preserved, even though they are supposed to be discarded via implicit ENDLOCAL when batch processing ends. See my DosTips post SETLOCAL continues after batch termination! for more information.

根据信息在为什么非交互式批处理脚本想我已经pressed控制-C?,我发现了一个干净的方式从被调用函数或脚本中退出所有批处理脚本和SETLOCAL之后所有的改变都正确丢弃。

Based on information at Why does a non-interactive batch script think I've pressed control-C?, I have discovered a clean way to exit all batch scripting from within a CALLed routine or script, and all changes after SETLOCAL are properly discarded.

@echo off
setlocal
set test=AFTER main SETLOCAL
call :sub
echo returning from main  NEVER REACHED
exit /b

:sub
setlocal
set test=AFTER sub SETLOCAL
set test
call :ExitBatch
echo returning from sub2 NEVER REACHED
exit /b


:ExitBatch - Cleanly exit batch processing, regardless how many CALLs
if not exist "%temp%\ExitBatchYes.txt" call :buildYes
call :CtrlC <"%temp%\ExitBatchYes.txt" 1>nul 2>&1
:CtrlC
cmd /c exit -1073741510

:buildYes - Establish a Yes file for the language used by the OS
pushd "%temp%"
set "yes="
copy nul ExitBatchYes.txt >nul
for /f "delims=(/ tokens=2" %%Y in (
  '"copy /-y nul ExitBatchYes.txt <nul"'
) do if not defined yes set "yes=%%Y"
echo %yes%>ExitBatchYes.txt
popd
exit /b

下面是运行上面的test.bat的示例输出。你可以看到,该脚本从不返回:一次批处理终止ExitBatch调用,并测试变量的定义已被正确丢弃

Here is sample output of running the above test.bat. You can see that the script never returned from the :ExitBatch call, and the test variable definition has been properly discarded once batch processing terminates.

C:\test>test.bat
test=AFTER sub SETLOCAL

C:\test>set test
Environment variable test not defined

C:\test>

的:ExitBatch程序可以放进自己的ExitBatch.bat脚本,你的路径,它可以方便地使用任何批处理脚本内的某处放置

The :ExitBatch routine can be put into its own ExitBatch.bat script and placed somewhere within your PATH such that it can be conveniently used by any batch script.

@echo off
:ExitBatch - Cleanly exit batch processing, regardless how many CALLs
if not exist "%temp%\ExitBatchYes.txt" call :buildYes
call :CtrlC <"%temp%\ExitBatchYes.txt" 1>nul 2>&1
:CtrlC
cmd /c exit -1073741510

:buildYes - Establish a Yes file for the language used by the OS
pushd "%temp%"
set "yes="
copy nul ExitBatchYes.txt >nul
for /f "delims=(/ tokens=2" %%Y in (
  '"copy /-y nul ExitBatchYes.txt <nul"'
) do if not defined yes set "yes=%%Y"
echo %yes%>ExitBatchYes.txt
popd
exit /b

重要更新:

现在可以实现健壮的异常处理用纯批次。您不仅可以丢,可以从任何CALL级别终止所有的批量处理的异常,但你也可以赶上在较高水平外,excecute特殊的异常处理清理code和恢复处理,或继续通过其他退出扔!请参见难道Windows批处理支持异常处理?获取更多信息。

It is now possible to implement robust exception handling with pure batch. Not only can you throw an exception that can terminate all batch processing from any CALL level, but you can also catch the exception at a higher level, excecute special exception handling cleanup code, and resume processing, or continue to exit via another throw! See Does Windows batch support exception handling? for more info.

这篇关于从一个函数内部退出批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆