[英]How to escape commands when comparing strings in batch file?
I noticed that string comparison in a batch file doesn't work properly if you compare against words like "IF" and "DO". 我注意到,如果与“ IF”和“ DO”之类的词进行比较,则批处理文件中的字符串比较将无法正常工作。
IF "DO" == "DO" (
ECHO YES
)
The above works fine, but isn't useful. 上面的工作正常,但没有用。
SET stringDO=DO
IF %stringDO% == "DO" (
ECHO YES
) ELSE (
ECHO NO
)
When we use a variable, the result for the above example is "NO" 当我们使用变量时,以上示例的结果为“ NO”
Strangely, comparing 2 variables works fine. 奇怪的是,比较2个变量可以正常工作。
SET stringDO=DO
SET compare=DO
IF %stringDO% == %compare% (
ECHO YES
)
So my question is, am I doing anything wrong or is this an intended behavior? 所以我的问题是,我做错了什么吗?或者这是预期的行为?
Is there another way to escape command words in string comparisons? 还有另一种在字符串比较中转义命令字的方法吗?
Question code if
comparison evaluated in order: 问题代码,
if
按顺序评估比较:
IF "DO" == "DO"
IF DO == "DO"
IF DO == DO
Double quotes are included in the comparison. 比较中包括双引号。
DO
does not equal "DO"
. DO
不等于"DO"
。
SET stringDO=DO
IF "%stringDO%" == "DO" (
ECHO YES
) ELSE (
ECHO NO
)
results in YES
. 结果为
YES
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.