代码之家  ›  专栏  ›  技术社区  ›  Ahmad Addas

在Windows批处理中用echo转义if语句中的点

  •  0
  • Ahmad Addas  · 技术社区  · 2 年前

    我遇到了一个先有鸡后有蛋的问题:我需要在批处理脚本中的if语句中回显一个点,但命令提示符崩溃,显示“”。这在当时是出乎意料的。”

    请注意,回显将所有内容都视为文字,但if语句将其解释为不正确,因为点是一个特殊字符。

    添加引号可以解决问题,但我不希望批处理用户看到引号的错误。我试过使用插入符号 ^ 角色来逃避它,但它不起作用。

    这是我的剧本:

    if "%found_7zip%"=="0" (
        echo 7-Zip or 7-Zip-ZStandard not found in Program Files or Program Files (x86).
        echo Please install 7-Zip or 7-Zip-ZStandard and try again.
        pause
        exit /b 1
    )
    

    非常感谢您的帮助!非常感谢。

    1 回复  |  直到 2 年前
        1
  •  1
  •   jeb    2 年前

    解决方法是转义右括号。

    echo 7-Zip or 7-Zip-ZStandard not found in Program Files or Program Files (x86^).
    

    这是必要的,因为中的左括号 if "%found_7zip%"=="0" ( 开始扫描第一个右括号,与行中的位置无关。
    它可以通过插入符号或双引号转义

        2
  •  -1
  •   kenny    2 年前

    我找到了答案 in another SO question.

    @echo|set /p="Please install 7-Zip or 7-Zip-ZStandard and try again."