【问题标题】:Check equivalence of variable in FOR /f loop batch在 FOR /f 循环批处理中检查变量的等价性
【发布时间】:2020-12-27 09:04:17
【问题描述】:

这次我尽量说清楚点!

我正在编写一个 .bat 文件来编译不同支持语言(fortran、C、C++ 等)的单个(目前)文件。由于目前它是针对单个文件的,因此我采用了这种架构:

buildfile [-lang] filename

如果指定 -lang 可以是 -cpp、-c、-for 等。如果未指定,-lang 将假定为文件扩展名。

现在,我报告第一段代码(最开始,所以之前什么都没有):

@echo off


:: check first input
if "%1"=="" goto :syntax
if "%1"=="-h" goto :syntax
if "%1"=="/h" goto :syntax
if "%1"=="/?" goto :syntax
if "%1"=="--help" goto :syntax
if "%1"=="/help" goto :syntax

echo %1 | findstr "^-" > inp.log
echo Not found >> inp.log
set "var="
for /f "tokens=* delims=" %%i in (inp.log) do (
    echo Big I writes %%i
    set "var=%%i"
    set var
    if "%var%"=="Not found" (
        echo String not found
        goto :end
        if "%~x1"=="" goto :syntax
    )
    goto :end
)

检查用户是否请求帮助后,我想检查字符“-”是否存在(这意味着是否指定了-lang)。

  1. 一开始我想重定向echo %1 | findstr "^-" > %avariable% 然后if "%avariable%"=="" 然后没有指定字符“-”,因此去检查带有"%~x1" 的文件扩展名(DID NOT WORK)。

  2. 其次,我想将 findstr 命令直接作为 FOR /F 循环的参数放置在回显 %1 中,但如果“-”不存在,则由于搜索字符串为空而爆炸! (即for /f "tokens=* delims=" %%i in (' echo %1 ^| findstr "^-" ') do (

所以,最后是您在这段代码中看到的,将输出写入文件并重新读取它,但是有些东西不能正常工作。 我添加了“未找到”行以避免读取空文件(因为显然给出了与选项 2 相同的错误)。 我看到当我设置 var 时,我正确地看到“var=Not found”,这意味着 var 设置正确。 但是,一旦我到达 FOR /F 循环内的 IF 条件,那将不起作用。

我可以想象存在一个更好、更清洁的解决方案,所以我在这里寻求您的帮助。 我会说离选项 1 不远的同样的东西可能是最好的,因为你只做 2 次操作(重定向,然后是 IF 条件),也许我错过了一些让它工作的语法。

非常感谢!

编辑: 当然,如果找到“-”字符,那么我会做一个简单的拼写检查来假设语言(通过许多 IF 语句)

PS:所有 goto 都在那里作为调试。

【问题讨论】:

  • echo %1 | findstr "^-" > inp.log%1 的内容加一个空格 写入文件。所以if "%1"=="-cpp" 的结果是if -cpp "=="-cpp",这不可能是真的。删除 te 管道之前的空间,就像您稍后使用 echo Not found>> inp.log 所做的那样(或更安全:(echo/%1) | findstr....
  • (PS:你的第一个版本有一个delayed expansion issue - 只是为了完整性)
  • @Stephan 非常感谢您的回复。我在尝试调试时注意到的第一句话(我在 inp.log 文件中看到 -cpp 或 Not found 之后有一个空白字符,因此尝试删除 echo %1| 之后的空格)。 W.R.T.第二句话,我可以保证我尝试了所有可能的解决方案(包括添加本地 ENABLEDELAYEDEXPANSION,但也没有工作..)。关于echo/%1,能不能说的温和一点?谢谢
  • @Stephan btw, echo %1 | findstr "^-" > inp.log 实际上并不受您下划线的影响(我为此感谢您,这是对我知识的补充),因为在这里我只想找到“- ”。事实上,在后一种解决方案中,这属于 ELSE 条件。在其中,我切换到 %1 (然后不包含输入错误的空格)。你同意吗?
  • 如果%1 为空,echo %1 将导致echo is OFF/(没有空格!)可以解决这个问题。 (注意:这个技巧最安全的字符是(echo(%emptyVar%),但它可能会与实际的“代码块开始”混淆,所以我更喜欢安全性稍差的/)。 Setlocal enabledelayed expansion 只是启用它,要实际使用它,您需要使用!var! 而不是%var%

标签: for-loop batch-file findstr


【解决方案1】:

编辑编辑:

看来我解决了删除 var 变量的问题,直接使用 %%i 一个:

@echo off


:: check first input
if "%1"=="" goto :syntax
if "%1"=="-h" goto :syntax
if "%1"=="/h" goto :syntax
if "%1"=="/?" goto :syntax
if "%1"=="--help" goto :syntax
if "%1"=="/help" goto :syntax

echo %1 | findstr "^-" > inp.log
echo Not found>> inp.log
set "var="
for /f "tokens=* delims=" %%i in (inp.log) do (
    echo Big I writes %%i
    if "%%i"=="Not found" (
        echo String not found
        echo %~x1 
        if "%~x1"=="" goto :syntax
    ) else (
        echo String found!
        if "%1"=="-cpp" shift & goto :cppfile
        if "%1"=="-c" shift & goto :cppfile
        if "%1"=="-for" shift & goto :fortranfile
        :: if one comes here, format not supported.
        goto :syntax
    )
)

【讨论】:

    猜你喜欢
    • 2016-04-14
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多