[英]Batch - Variable giving different outputs at ECHO and IF
I am currently implementing a postprocess in batch, and after some changes a few code snipplets simply don't work anymore.... 我目前正在批量实现一个后期处理,经过一些更改后,一些代码snipplet根本不再工作....
What I am trying to do: 我想做什么:
@echo off & setlocal
ECHO %netUse% //For debugging!
IF NOT "%netUse%" == "" ( //Double checking if Variable is set
IF %netUse% EQU 1 (
IF %netUsePW% EQU 1 (
NET USE %netUseLetter% %netPath% %pw% /user:%netUser%
)
IF %netUsePW% EQU 0 (
NET USE %netUseLetter% %netPath%
)
)
)
When I run that code, this is the output: 当我运行该代码时,这是输出:
C:\Program Files (x86)\ID GmbH\PRODETTE\DAX\ISDN>ECHO 0
0 <--- That's right! It is set to ZERO via a "configuration" Batch
"1" kann syntaktisch an dieser Stelle nicht verarbeitet werden. <-- ???
C:\Program Files (x86)\ID GmbH\PRODETTE\DAX\ISDN> IF EQU 1 ( <-- Why isn't it comparing the variable?
And just for completionists sake, I am loading all of these variables form another batch file, "Settings.bat" I am running that file via CALL :postCopySettings.bat 为了完成者的缘故,我将所有这些变量加载到另一个批处理文件中,“Settings.bat”我通过CALL运行该文件:postCopySettings.bat
contents: 内容:
SET netUse=0
SET netUsePW=
SET netUseLetter=
SET netPath=
SET netUser=
SET pw=
SET configLoaded=1
(Since netUse is 0, I don't need to populate the other variables... right?) (由于netUse为0,我不需要填充其他变量......对吗?)
TL:DR: Batch doesn't work, but I don't know why. TL:DR:批处理不起作用,但我不知道为什么。
IF NOT "%netUse%" == "" ( //Double checking if Variable is set
IF %netUse% EQU 1 (
IF %netUsePW% EQU 1 (
No point in testing "netuse" then complaining that "netusePW" is not set. 没有必要测试“netuse”然后抱怨没有设置“netusePW”。
The problem is that variable netUsePW
is not set. 问题是没有设置变量netUsePW
。
When netUse
is set to 0
, and netUsePW
, netUseLetter
, netUsePath
, pw
are all empty, your code...: 当netUse
设置为0
, netUsePW
, netUseLetter
, netUsePath
, pw
都为空时,你的代码......:
@echo off & setlocal
ECHO %netUse%
IF NOT "%netUse%" == "" (
IF %netUse% EQU 1 (
IF %netUsePW% EQU 1 (
NET USE %netUseLetter% %netPath% %pw% /user:%netUser%
)
IF %netUsePW% EQU 0 (
NET USE %netUseLetter% %netPath%
)
)
)
...expands to...: ...扩展到......:
@echo off & setlocal
ECHO 0
IF NOT "0" == "" (
IF 0 EQU 1 (
IF EQU 1 (
NET USE /user:0
)
IF EQU 0 (
NET USE
)
)
)
As you can see, there are two syntax issues, which cause the error: 如您所见,有两个语法问题,导致错误:
IF EQU 1 IF EQU 0
Although netUse
is set to 0
and you therefore expect the statements after IF %netUse% EQU 1
not to be executed, they are still parsed, because the entire code block is parsed before any commands are executed. 虽然netUse
设置为0
,因此您希望IF %netUse% EQU 1
之后的语句不被执行,但它们仍然被解析,因为在执行任何命令之前解析整个代码块。
To overcome this, there are several ways: 为了克服这个问题,有几种方法:
Preset variables with non-empty values: 使用非空值预设变量:
@echo off & setlocal ECHO %netUse% IF "%netUse%" == "" (set "netUse=-1" & set "setUsePW=-1") IF NOT "%netUse%" == "" ( IF %netUse% EQU 1 ( IF %netUsePW% EQU 1 ( NET USE %netUseLetter% %netPath% %pw% /user:%netUser% ) IF %netUsePW% EQU 0 ( NET USE %netUseLetter% %netPath% ) ) )
Avoid code blocks by changing the logic a bit and using goto
: 通过改变逻辑并使用goto
避免代码块:
@echo off & setlocal ECHO %netUse% IF "%netUse%" == "" goto :SKIP IF %netUse% NEQ 1 goto :SKIP IF %netUsePW% EQU 1 ( NET USE %netUseLetter% %netPath% %pw% /user:%netUser% ) IF %netUsePW% EQU 0 ( NET USE %netUseLetter% %netPath% ) :SKIP
Avoid code blocks by moving some code into subroutines and using call
to call them: 通过将一些代码移动到子程序中并使用call
来调用它们来避免代码块:
@echo off & setlocal ECHO %netUse% IF NOT "%netUse%" == "" call :TEST "%netUse%" "%netUsePW%" "%netUseLetter%" "%netPath%" "%pw%" exit /B :TEST IF %~1 EQU 1 call :TESTPW "%~1" "%~2" "%~3" "%~4" "%~5" exit /B :TESTPW IF %~2 EQU 1 ( NET USE %~3 %~4 %~5 /user:%~1 ) IF %~2 EQU 0 ( NET USE %~3 %~4 ) exit /B
Enable and apply delayed expansion to do the variable expansion during execution rather than while parsing: 启用并应用延迟扩展以在执行期间执行变量扩展,而不是在解析时:
@echo off & setlocal EnableDelayedExpansion ECHO %netUse% IF NOT "%netUse%" == "" ( IF !netUse! EQU 1 ( IF !netUsePW! EQU 1 ( NET USE %netUseLetter% %netPath% %pw% /user:!netUser! ) IF !netUsePW! EQU 0 ( NET USE %netUseLetter% %netPath% ) ) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.