[英]ECHO is not showing variables, even though they appear in SET - Windows Batch File
In some code I'm working on, I need to connect to a terminal, which requires a terminal's IP address. 在我正在处理的一些代码中,我需要连接到终端,这需要终端的IP地址。 Some of the code is here: 一些代码在这里:
FOR /F "Tokens=1" %%A IN (%IPLIST%) DO (
NET USE //%%A /user:name password
SET TERMINAL=%%A
ECHO %TERMINAL%
)
However, after doing this, Echo responds with: 但是,在这样做之后,Echo回应:
ECHO is off
As a test, I simply added SET after the ECHO %TERMINAL% Line, and according to it: 作为测试,我只是在ECHO%TERMINAL%Line之后添加SET,并根据它:
TERMINAL=10.123.45.6
So, my dilemma is that, according to SET, TERMINAL does indeed have a value, but ECHO doesn't agree. 所以,我的困境是,根据SET,TERMINAL确实有价值,但ECHO不同意。
The reason this is so important is because I cannot tell if the value is correct for future use in the rest of the code, where I implement it to enact different batches in a remote system. 之所以如此重要,是因为我无法判断该值是否正确,以备将来在其余代码中使用,我将其实现为在远程系统中实现不同的批处理。
Any help would be much appreciated! 任何帮助将非常感激!
I belive the reason is that FOR
is evaluated as a single command and the environment variables are expanded before execution, so in your case it will be run like this: 我相信原因是FOR
被评估为单个命令并且环境变量在执行之前被扩展,因此在您的情况下它将像这样运行:
FOR /F "Tokens=1" %A IN (<%IPLIST% value>) DO (
NET USE //%A /user:name password
SET TERMINAL=%A
ECHO <%TERMINAL% value>
)
Since %TERMINAL%
didn't exist before FOR
, it'll be expanded to an empty string, hence the observed behaviour. 由于%TERMINAL%
在FOR
之前不存在,因此它将扩展为空字符串,因此观察到的行为。
Try running the script with @echo on
, it helps understand what's going on. 尝试使用@echo on
运行脚本,它有助于了解正在发生的事情。
You might be able to get the behaviour you want with Setlocal EnableDelayedExpansion
, see this link for more details. 您可以通过Setlocal EnableDelayedExpansion
获得所需的行为,有关详细信息,请参阅此链接 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.