为什么我的set命令导致什么都没有存储? [英] Why are my set commands resulting in nothing getting stored?

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

问题描述

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

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

if exist "%_REALPATH%\tomcat-%TOMCAT_VER2%" (
  set CATALINA_HOME=%_REALPATH%\tomcat-%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语句,但是它似乎不起作用.禁用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!\tomcat-!TOMCAT_VER2!" (
  set "CATALINA_HOME=!_REALPATH!\tomcat-!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!
)

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

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