为什么我的设置命令没有存储任何内容? [英] Why are my set commands resulting in nothing getting stored?

查看:16
本文介绍了为什么我的设置命令没有存储任何内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我稍后尝试访问 TOMCAT_VER 的值,但它显示为空字符串.

I am trying to access the value of TOMCAT_VER later on, but it appears as an empty string.

if exist "%_REALPATH%	omcat-%TOMCAT_VER2%" (
  set CATALINA_HOME=%_REALPATH%	omcat-%TOMCAT_VER2%
  set TOMCAT_VER=%TOMCAT_VER2%
  echo "%TOMCAT_VER%"
) else if exist "%TOMCAT_VER2%" (
  set CATALINA_HOME="%TOMCAT_VER2%"
  set TOMCAT_VER="%TOMCAT_VER2%"
  echo "%TOMCAT_VER%"
)

为了进一步调试,我在它设置的正下方插入了一个 echo 语句,但它似乎不起作用.禁用回显后,我可以看到显示这些变量正在设置的语句,但我似乎无法将它们打印出来.

To further debug, I inserted an echo statement right below where it gets set, but it doesn't seem to work. With echo off disabled, I can see the statement showing these variables getting set, and yet I can't seem to print them out.

推荐答案

您发现了 bbb(批处理初学者错误),但不是变量为空,而是扩展没有按预期工作.

You found the bbb (batch beginner bug), but not the variable is empty, it's the expansion that doesn't work as expected.

在解析一行或一个完整的括号块时,在执行代码之前完成百分比扩展.
但是要解决这个问题,您可以使用延迟扩展,它不会在解析时扩展,它只会在执行时扩展.

Percent expansion is done when a line or a complete parenthesis block is parsed, before the code will be executed.
But to solve this you can use the delayed expansion, this doesn't expand at parse time, it expands just at execution time.

EnableDelayedExpansion 添加额外的语法来扩展变量:!var!.
百分比扩展 %var% 仍然可用,不会因延迟扩展而改变.
!var! 的延迟扩展在表达式执行时完成,尽管 %var% 会被扩展在解析的那一刻(完整的代码块),在块中的任何命令被执行之前.

EnableDelayedExpansion adds an additional syntax to expand variables: !var!.
The percent expansion %var% is still availabe and isn't changed by the delayed expansion.
The delayed expansion of !var! is done when the expression is executed, in spite of %var%, that will be expanded in the moment of parsing (complete code blocks), before any of the commands in the blocks are executed.

setlocal EnableDelayedExpansion

if exist "!_REALPATH!	omcat-!TOMCAT_VER2!" (
  set "CATALINA_HOME=!_REALPATH!	omcat-!TOMCAT_VER2!"
  set "TOMCAT_VER=!TOMCAT_VER2!"
  echo !TOMCAT_VER!
) else if exist "!TOMCAT_VER2!" (
  set "CATALINA_HOME=!TOMCAT_VER2!"
  set "TOMCAT_VER=!TOMCAT_VER2!"
  echo !TOMCAT_VER!
)

这篇关于为什么我的设置命令没有存储任何内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆