jeb 的致命语法错误方法有效,但它不能正确终止任何活动的 SETLOCAL。
例如,这里是 fatalTest.bat:
@echo off
setlocal enableDelayedExpansion
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
call :__exitBatch 2>nul
:__ExitBatch
for
这里是示例运行输出,展示了如何在批处理终止后仍然定义测试和延迟扩展:
C:\test>fatalTest
test=AFTER sub SETLOCAL
C:\test>echo !test!
AFTER sub SETLOCAL
C:\test>
现在无法将环境恢复到批处理前状态。应该终止 CMD.EXE 会话。
这是一个改进的 CtrlCTest.bat 版本,可以在批处理终止时正确终止任何活动的 SETLOCAL。它使用了一种令人惊讶的技术来以编程方式“按下”Ctrl-C,我在 Why does a non-interactive batch script think I've pressed control-C? 了解到这一点
@echo off
setlocal enableDelayedExpansion
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
这是示例运行输出。可以看到延迟扩展不再活跃,测试也不再定义:
C:\test>CtrlCTest
test=AFTER sub SETLOCAL
C:\test>echo !test!
!test!
C:\test>set test
Environment variable test not defined
C:\test>
您可以将 :ExitBatch 例程放在独立的 ExitBatch.bat 脚本中,该脚本位于 PATH 中的某个位置,然后任何批处理都可以根据需要方便地调用该实用程序。
@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