DOS批处理:为什么造成什么我的命令集获得存储? [英] DOS batch: Why are my set commands resulting in nothing getting stored?

查看:100
本文介绍了DOS批处理:为什么造成什么我的命令集获得存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想稍后访问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语句下方那里得到确定,但它似乎并没有工作。与之呼应了残疾人,我能看到的声明显示出越来越设置这些变量,但我似乎无法打印出来。

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.

在一条线或一个完整的括号块被解析,扩张百分比完成将要执行的code之前。结果
但要解决这个问题,你可以使用延迟扩展,但这不是在分析时展开,它只是在执行时扩大了。

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.

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!"
)

这篇关于DOS批处理:为什么造成什么我的命令集获得存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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