windows - 如何“打破” For 循环?
问题描述
下面,当我执行goto
命令时,它只是挂起,我必须控制-C。我也试过EXIT /b
。我试图尽可能地避免 goto。有没有办法做我想做的事?
:SUB_bigRandLooper
set /a lcv=0
FOR /L %%s IN ( 0 , 0 , 1 ) DO (
set big-rand=!random:~-4!
echo big-rand is !big-rand!
set /a lcv=%lcv+1
if !big-rand! GTR 9900 goto bigRandLooperWrapup
)
:bigRandLooperWrapup
echo biggest-rand is %big-rand%
echo lcv is %lcv%
EXIT /B
.
解决方案
简短的回答是:不,你不能。
由于您正在使用for /L
并建立一个无限循环,并且循环在执行之前已被预处理和缓存,因此它不能被goto
;中断。goto
打破循环上下文,或者更准确地说,(
/)
块上下文,因此块中不再执行命令,但循环本身仍在运行。
您可以通过以下代码证明这一点:
for /L %%I in (1,1,100000) do (
echo %%I
if %%I equ 10 goto :SKIP
)
:SKIP
echo skipped
你会看到echo %%I
它只对%%I
from 1
to10
执行,但执行不会立即继续,echo skipped
但有一个明显的延迟,因为循环在后台完成迭代,尽管没有执行更多命令。
不过有一个解决方法:您可以使用 建立一个无限循环goto
,如下所示:
:SUB_bigRandLooper
set /A lcv=0
:bigRangLooperLoop
set big-rand=!random:~-4!
echo big-rand is !big-rand!
set /A lcv+=1
if !big-rand! gtr 9900 goto :bigRandLooperWrapup
goto :bigRangLooperLoop
:bigRandLooperWrapup
echo biggest-rand is %big-rand%
echo lcv is %lcv%
exit /B
我知道goto
循环比循环慢for /L
,但这是创建可破坏无限循环的唯一方法。
一种更快的方法是嵌套两个循环方法:使用for /L
迭代几千次并环绕一个无限goto
循环。
另一种解决方法是利用exit
命令可以中断(无限)for /L
循环的事实。但是由于这也会退出cmd
运行批处理文件的实例,因此需要将循环放入单独的cmd
实例中。当然,环境与当前环境完全分离。解决方案可能如下所示:
:SUB_bigRandLooper
@echo off
rem // Check for argument, interpret it as jump label if given:
if not "%~1"=="" goto %~1
rem /* Establish explicit `cmd` instance for executing the `for /L` loop;
rem the `for /F` loop implicitly creates the new `cmd` instance for the command
rem it executes, so we do not have to explicitly call `cmd /C` here; the resulting
rem values are echoed by the sub-routine and captured here by `for /F`: */
for /F "tokens=1,2" %%I in ('"%~f0" :bigRandLooperLoop') do (
rem // Assign captured values to environment variables:
set "big-rand=%%I" & set "lcv=%%J"
)
:bigRandLooperWrapup
echo biggest-rand is %big-rand%
echo lcv is %lcv%
exit /B
:bigRandLooperLoop
setlocal EnableDelayedExpansion
set /A lcv=0
for /L %%s in (0,0,1) do (
set big-rand=!random:~-4!
rem /* Explicitly redirect this output to the console window to prevent it from
rem being captured by `for /F` in the main routine too: */
> con echo big-rand is !big-rand!
set /A lcv+=1
if !big-rand! gtr 9900 (
rem // Output found values in order to be able to capture them by `for /F`:
echo !big-rand! !lcv!
rem // Break loop and leave current `cmd` instance:
exit
)
)
endlocal
推荐阅读
- python - 在 Popen 中将标准输出(或标准输入)设置为 subprocess.PIPE 究竟意味着什么?
- python - Selenium:此版本的 ChromeDriver 仅支持 Chrome 版本 83
- scala - scala代码通过使用小部件传递动态值来读取镶木地板文件
- dependency-injection - 在 Jersey Servlet 中使用 @Inject,用于实现 Auth,Auth 过滤器传递一个 User 对象,给出 UnsatisfiedDependencyException
- angular - Ag 网格 - this.gridapi.foreachnode 不打印任何内容
- assembly - 英特尔 8086 组件,试图理解标签。问题
- database - 如何在 postgresql 中使用 jsonb 数组条目在表上创建视图
- javascript - 多个页面上的 Firebase SDK 中断数据库引用
- excel - 我可以在 Excel 数据透视表中以不同方式过滤两列吗?
- jboss - Jboss EAP 7 server.log 和应用程序日志的问题