Windows中的圆括号不能包含cmd-script变量值? [英] Parenthesis in Windows cmd-script variable values not allowed?
问题描述
为什么给出以下Windows 7 .cmd命令脚本:
Why gives the following Windows 7 .cmd command script:
set SUN_JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_17
if 3==3 (
set JAVA_HOME=%SUN_JAVA_HOME%
)
echo ready
以下错误消息,而不是打印就绪
The following error message instead of printing "ready"
\Java\jdk1.6.0_17 was unexpected at this time.
如果我删除路径名中的(x86),错误消息就会消失。 p>
The error message disapears, if I remove the "(x86)" in the path name.
推荐答案
问题是 if 3 == 3
部分后的括号分组
The problem is the parentheses grouping after the if 3==3
part.
当解析 set JAVA_HOME =%SUN_JAVA_HOME%
命令时,解释器立即替换%SUN_JAVA_HOME%
变量,这会导致(386)
中右括号的早期匹配。
While parsing the set JAVA_HOME=%SUN_JAVA_HOME%
command, the interpreter immediately replaces the %SUN_JAVA_HOME%
variable and that causes an early match of the closing parenthesis in (386)
.
如果启用延迟了扩展,并将%SUN_JAVA_HOME%
替换为!SUN_JAVA_HOME!
:
This can be avoided if you enable delayed expansion and replace %SUN_JAVA_HOME%
with !SUN_JAVA_HOME!
:
setlocal enabledelayedexpansion
set SUN_JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_17
if 3==3 (
set JAVA_HOME=!SUN_JAVA_HOME!
)
echo ready
这篇关于Windows中的圆括号不能包含cmd-script变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!