在for循环内进行设置时,变量设置表现出意外的行为 [英] Variable setting exhibits unexpected behavior when set inside for loop

查看:118
本文介绍了在for循环内进行设置时,变量设置表现出意外的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改批处理脚本,以将文件位置传递给makefile(使用nmake运行). for循环应该在可用驱动器上循环并搜索文件(如果之前未设置).批处理不是我有很多经验的东西,所以希望有人可以发现发生的事情.

I am modifying a batch script to pass a file location to a makefile (run with nmake). The for loop is supposed to loop over available drives and search for the file if it hasn't been previously set. Batch isn't something I have much experience with, so hopefully someone can spot what's going on.

当我在遍历驱动器的for循环之外设置winbison时,将使用win_bison.exe的路径来设置变量.当我在for循环中设置它时,我得到"ECHO on".我认为这是批处理如何解析/扩展的征兆.我设置了EnableDelayedExpansion,但是得到了相同的结果.

When I set winbison outside of the for loop that iterates over the drives, the variables get set with the path to win_bison.exe. When I set it inside the for loop, I get "ECHO is on". I thought that was a sympton of how batch handles parsing/expansion. I set EnableDelayedExpansion but got the same result.

这是我的代码.

@ECHO OFF
setlocal EnableDelayedExpansion
set currentDir=%cd%
set winbison=
set winflex=

set drives=
for /f "delims=" %%a in ('fsutil fsinfo drives') do @set drives=%%a

REM :~8 is to slice off "Drives: " returned by fsutil
for %%i in (%drives:~8%) do (
    chdir /d %%i
    if not defined [%winbison%] (
        set winbison=
        for /f "delims=" %%a in ('dir win_bison.exe /s /b 2^>nul') do @set winbison=%%a
    @ECHO ON
        echo test
        echo %winbison%
    @ECHO OFF
    )

    if not defined [%winflex%] (
        set winflex=
        for /f "delims=" %%a in ('dir win_flex.exe /s /b 2^>nul') do @set winflex=%%a
    )
)

chdir /d %currentDir%

@ECHO ON
echo %winbison%
... stuff gets passed to nmake.

推荐答案

下一个脚本可以工作:

@ECHO OFF
setlocal enableextensions EnableDelayedExpansion
set "currentDir=%cd%"
set "winbison="
set "winflex="

set "drives="
for /f "delims=" %%a in ('fsutil fsinfo drives') do @set "drives=%%a"

REM :~8 is to slice off "Drives: " returned by fsutil
for %%i in (%drives:~8%) do (
  if exist %%iNUL (
    pushd %%i
    if not defined winbison (
      for /f "delims=" %%a in (
        'dir win_bison.exe /s /b 2^>nul') do @set "winbison=%%a"
      echo [debug] %%i winbison=!winbison!
    )

    if not defined winflex (
      for /f "delims=" %%a in (
        'dir win_flex.exe /s /b 2^>nul') do @set "winflex=%%a"
      echo [debug] %%i winflex=!winflex!
    )
    popd
  )
)

echo [debug] winbison=%winbison%
echo [debug] winflex=%winflex%

rem ... stuff gets passed to nmake

在上面的代码段中:

    在所有set "variable=value"中使用
  • 引号,以避免(意外地忘记)尾随空格;
  • if exist %%iNUL以避免可能的The device is not ready错误消息;
  • 正确的语法if not defined winbison等;
  • !variable!代替必要的%variable%(请阅读 EnableDelayedExpansion );
  • 使用
  • pushd ... popd对代替cd /D.
  • quotes in all set "variable=value" to avoid (accidentally forgotten) trailing spaces;
  • if exist %%iNUL to avoid possible The device is not ready error message;
  • right syntax if not defined winbison etc.;
  • !variable! instead of %variable% where necessary (read EnableDelayedExpansion);
  • pushd ... popd pair used instead of cd /D.

这篇关于在for循环内进行设置时,变量设置表现出意外的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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