some_word"?-6ren"> some_word"?-示例如下: @echo off :: check syntax ignores "^something" right after check expression if defined path^ b-6ren">
gpt4 book ai didi

batch-file - 为什么 IF 检查在检查表达式之后会忽略 "^some_word"?

转载 作者:行者123 更新时间:2023-12-04 01:10:25 25 4
gpt4 key购买 nike

示例如下:

@echo off
:: check syntax ignores "^<delimiter>something" right after check expression
if defined path^ blah echo #
if exist c:\^ blah echo #
if errorlevel 0^ blah echo #
:: but in comparison operations escaping works well
if 1^ blah==1^ blah echo #

最佳答案

因为它是这样工作的;-)

在我看来,这是 IF 命令的设计缺陷。 IF 命令有复杂的(多阶段)解析规则,我认为微软有点落伍了。

Escape 不适用于 IF EXIST 或 IF DEFINED。第一个解析过程正确识别条件和命令,但实际比较在第一个空格处停止。

引号与 IF EXIST 一起使用。但是引号对 IF DEFINED 没有帮助,因为它们被认为是变量名的一部分。

让它在所有情况下都能正常工作的唯一方法是使用 FOR 变量或延迟扩展:

@echo off
setlocal enableDelayedExpansion
set "var=%%%%~A"

:: Start with a clean slate
set "a="
set "a z="
del "a" "a z" 2>nul

:: ---------- test escape --------------
:: This fails
set "a z=1"
if defined a^ z (
echo if defined a^^ z TRUE = SUCCESS
) else (
echo if defined a^^ z FALSE = FAILURE
)
set "a z="

:: This fails
copy nul "a z" >nul
if exist a^ z (
echo if exist a^^ z TRUE = SUCCESS
) else (
echo if exist a^^ z FALSE = FAILURE
)
del "a z"

:: This fails (looks at the wrong variable)
set "a=1"
if defined a^ z (
echo if defined a^^ z TRUE = FAILURE
) else (
echo if defined a^^ z FALSE = SUCCESS
)
set "a="

:: This fails (looks at wrong file)
copy nul "a" >nul
if exist a^ z (
echo if exist a^^ z TRUE = FAILURE
) else (
echo if exist a^^ z FALSE = SUCCESS
)
del "a"

echo(

:: ---------- test quotes --------------
:: This fails
set "a z=1"
if defined "a z" (
echo if defined "a z" TRUE = SUCCESS
) else (
echo if defined "a z" FALSE = FAILURE
)
set "a z="

:: This succeeds
copy nul "a z" >nul
if exist "a z" (
echo if exist "a z" TRUE = SUCCESS
) else (
echo if exist "a z" FALSE = FAILURE
)
del "a z"

:: This fails (looks at the wrong variable with quotes in name)
set ""a z"=1"
if defined "a z" (
echo if defined "a z" TRUE = FAILURE
) else (
echo if defined "a z" FALSE = SUCCESS
)
set ""a z"="

:: This succeeds
copy nul "a" >nul
if exist "a z" (
echo if exist "a z" TRUE = FAILURE
) else (
echo if exist "a z" FALSE = SUCCESS
)
del "a"

echo(

:: ---------- test FOR variable --------------
:: This succeeds
set "a z=1"
for %%A in ("a z") do if defined %%~A (
echo if defined !var! TRUE = SUCCESS
) else (
echo if defined !var! FALSE = FAILURE
)
set "a z="

:: This succeeds
copy nul "a z" >nul
for %%A in ("a z") do if exist %%~A (
echo if exist !var! TRUE = SUCCESS
) else (
echo if exist !var! FALSE = FAILURE
)
del "a z"

:: This succeeds
set "a=1"
for %%A in ("a z") do if defined %%~A (
echo if defined !var! TRUE = FAILURE
) else (
echo if defined !var! FALSE = SUCCESS
)
set "a="

:: This succeeds
copy nul "a" >nul
for %%A in ("a z") do if exist %%~A (
echo if exist !var! TRUE = FAILURE
) else (
echo if exist !var! FALSE = SUCCESS
)
del "a"

echo(

:: ---------- test delayed expansion --------------
set "var=a z"

:: This succeeds
set "a z=1"
if defined !var! (
echo if defined ^^!var^^! TRUE = SUCCESS
) else (
echo if defined ^^!var^^! FALSE = FAILURE
)
set "a z="

:: This succeeds
copy nul "a z" >nul
if exist !var! (
echo if exist ^^!var^^! TRUE = SUCCESS
) else (
echo if exist ^^!var^^! FALSE = FAILURE
)
del "a z"

:: This succeeds
set "a=1"
if defined !var! (
echo if defined ^^!var^^! TRUE = FAILURE
) else (
echo if defined ^^!var^^! FALSE = SUCCESS
)
set "a="

:: This succeeds
copy nul "a" >nul
if exist !var! (
echo if exist ^^!var^^! TRUE = FAILURE
) else (
echo if exist ^^!var^^! FALSE = SUCCESS
)
del "a"

--输出--

if defined a^ z FALSE = FAILURE
if exist a^ z FALSE = FAILURE
if defined a^ z TRUE = FAILURE
if exist a^ z TRUE = FAILURE

if defined "a z" FALSE = FAILURE
if exist "a z" TRUE = SUCCESS
if defined "a z" TRUE = FAILURE
if exist "a z" FALSE = SUCCESS

if defined %%~A TRUE = SUCCESS
if exist %%~A TRUE = SUCCESS
if defined %%~A FALSE = SUCCESS
if exist %%~A FALSE = SUCCESS

if defined !var! TRUE = SUCCESS
if exist !var! TRUE = SUCCESS
if defined !var! FALSE = SUCCESS
if exist !var! FALSE = SUCCESS

关于batch-file - 为什么 IF 检查在检查表达式之后会忽略 "^<delimter>some_word"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28000194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com