gpt4 book ai didi

batch-file - `set VAR 2> nul` 后面的条件执行失败

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

set command使用变量名(前缀)设置 exit code如果没有定义匹配的变量,则为非零值;退出码可以通过conditional execution operators查询:

set VAR && echo Yes. || echo No!!

如果定义了 VAR(或 VARiable 等),上面的代码将返回 Yes.,而 No!! 否则,连同来自 set 命令的错误消息 Environment variable VAR not defined

但是,当我试图抑制错误消息时,No!! 总是被回显,与是否定义了 VAR 无关:

set VAR 2> nul && echo Yes. || echo No!!

当我在命令提示符或批处理文件中执行命令行时,结果是一样的。

为什么插入重定向部分 (2> nul) 会改变运算符 &&|| 收到的退出代码?

最佳答案

引用命令行:

set VAR 2> nul && echo Yes. || echo No!!

看来nul之间的SPACE&&被视为变量名的一部分,所以 set不检查名称以 VAR 开头的变量, 而不是 VAR + SPACE,显然没有定义。


我创建了一个包含大量测试的批处理文件,合并了有用的案例 comment按用户LotPings :

@echo on
@rem /* Execute test cases in a sub-routine twice,
@rem once with variable `VAR` defined and once not: */
@for %%I in ("Value" "") do @(
set "VAR=%%~I"
echo/& < nul set /P ="VARIABLE: "
if defined VAR (set VAR) else echo VAR=
call :SUB
)
@goto :EOF

:SUB
@rem // This constitutes a list of test cases:
@echo/& echo UNQUOTED (VAR):
set VAR && echo Yes. || echo No!!
set VAR> nul && echo Yes. || echo No!!
set VAR > nul && echo Yes. || echo No!!
set VAR 2> nul && echo Yes. || echo No!!
(set VAR > nul) && echo Yes. || echo No!!
(set VAR 2> nul) && echo Yes. || echo No!!
set VAR > nul&& echo Yes. || echo No!!
set VAR 2> nul&& echo Yes. || echo No!!
> nul set VAR && echo Yes. || echo No!!
2> nul set VAR && echo Yes. || echo No!!
@echo/& echo QUOTED ("VAR"):
set "VAR" && echo Yes. || echo No!!
set "VAR"> nul && echo Yes. || echo No!!
set "VAR" > nul && echo Yes. || echo No!!
set "VAR" 2> nul && echo Yes. || echo No!!
(set "VAR" > nul) && echo Yes. || echo No!!
(set "VAR" 2> nul) && echo Yes. || echo No!!
set "VAR" > nul&& echo Yes. || echo No!!
set "VAR" 2> nul&& echo Yes. || echo No!!
> nul set "VAR" && echo Yes. || echo No!!
2> nul set "VAR" && echo Yes. || echo No!!
@goto :EOF

这是相关的控制台窗口输出:

>>> test-set.bat

VARIABLE: VAR=Value

UNQUOTED (VAR):

>>> set VAR && echo Yes. || echo No!!
VAR=Value
Yes.

>>> set VAR 1>nul && echo Yes. || echo No!!
Yes.

>>> set VAR 1>nul && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> set VAR 2>nul && echo Yes. || echo No!!
No!!

>>> (set VAR 1>nul ) && echo Yes. || echo No!!
Yes.

>>> (set VAR 2>nul ) && echo Yes. || echo No!!
VAR=Value
Yes.

>>> set VAR 1>nul && echo Yes. || echo No!!
Yes.

>>> set VAR 2>nul && echo Yes. || echo No!!
VAR=Value
Yes.

>>> set VAR 1>nul && echo Yes. || echo No!!
Yes.

>>> set VAR 2>nul && echo Yes. || echo No!!
VAR=Value
Yes.

QUOTED ("VAR"):

>>> set "VAR" && echo Yes. || echo No!!
VAR=Value
Yes.

>>> set "VAR" 1>nul && echo Yes. || echo No!!
Yes.

>>> set "VAR" 1>nul && echo Yes. || echo No!!
Yes.

>>> set "VAR" 2>nul && echo Yes. || echo No!!
VAR=Value
Yes.

>>> (set "VAR" 1>nul ) && echo Yes. || echo No!!
Yes.

>>> (set "VAR" 2>nul ) && echo Yes. || echo No!!
VAR=Value
Yes.

>>> set "VAR" 1>nul && echo Yes. || echo No!!
Yes.

>>> set "VAR" 2>nul && echo Yes. || echo No!!
VAR=Value
Yes.

>>> set "VAR" 1>nul && echo Yes. || echo No!!
Yes.

>>> set "VAR" 2>nul && echo Yes. || echo No!!
VAR=Value
Yes.

VARIABLE: VAR=

UNQUOTED (VAR):

>>> set VAR && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> set VAR 1>nul && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> set VAR 1>nul && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> set VAR 2>nul && echo Yes. || echo No!!
No!!

>>> (set VAR 1>nul ) && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> (set VAR 2>nul ) && echo Yes. || echo No!!
No!!

>>> set VAR 1>nul && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> set VAR 2>nul && echo Yes. || echo No!!
No!!

>>> set VAR 1>nul && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> set VAR 2>nul && echo Yes. || echo No!!
No!!

QUOTED ("VAR"):

>>> set "VAR" && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> set "VAR" 1>nul && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> set "VAR" 1>nul && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> set "VAR" 2>nul && echo Yes. || echo No!!
No!!

>>> (set "VAR" 1>nul ) && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> (set "VAR" 2>nul ) && echo Yes. || echo No!!
No!!

>>> set "VAR" 1>nul && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> set "VAR" 2>nul && echo Yes. || echo No!!
No!!

>>> set "VAR" 1>nul && echo Yes. || echo No!!
Environment variable VAR not defined
No!!

>>> set "VAR" 2>nul && echo Yes. || echo No!!
No!!

唯一失败的情况是:

set VAR > nul && echo Yes. || echo No!!
set VAR 2> nul && echo Yes. || echo No!!

当命令解析器识别并暂时删除重定向部分(参见 How does the Windows Command Interpreter (CMD.EXE) parse scripts? 中第 2 阶段的描述)时,> 之前的潜在 SPACE和/或在 nul 之后被遗留下来,这似乎被 set 视为变量名称的一部分 | , 除非给定的变量名被引用;不过,总共只有一个 SPACE 似乎是可以容忍的。

另请查看错误消息 Environment variable VAR not defined变量名后面有不同数量的SPACE


set命令似乎以特定方式处理其参数:

@set "VAR=Value"
rem // No trailing spaces -- returns `VAR=Value`:
set VAR
rem // One trailing space -- returns `VAR=Value`:
set VAR
rem // Two or more trailing spaces -- returns an ERROR!
set VAR
rem // No trailing spaces -- returns `VAR=Value`:
set "VAR"
rem // One trailing space -- returns `VAR=Value`!?
set "VAR "
rem // Two or more trailing spaces -- returns an ERROR!
set "VAR "

不带引号的语法似乎不遵循标记化的标准规则,其中两个或多个连续的标记分隔符(如 SPACE)合并为一个。

令我大吃一惊的是,即使是引用的语法容差也是一个SPACE,尽管它忽略了结束" 之后的所有SPACE。至少。

更令人惊讶的是,在 SPACE 后面的一些其他文本(如 X)仍然没有返回任何错误:

@set "VAR=Value"
rem // No spaces behind `X`, one in front -- returns `VAR=Value`!?
set VAR X
rem // No spaces behind `X`, two in front -- returns an ERROR!
set VAR X
rem // No spaces behind `X`, one in front -- returns `VAR=Value`!?
set "VAR X"
rem // No spaces behind `X`, two in front -- returns an ERROR!
set "VAR X"

关于batch-file - `set VAR 2> nul` 后面的条件执行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57778895/

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