Windows 批处理文件中的 if 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9887826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:23:42  来源:igfitidea点击:

If statement in Windows Batch file

windowsbatch-filecmd

提问by c14kaa

I have been stuck on this silly if statement, whatever i do, I cannot get the if statment to go to the correct label.

我一直坚持这个愚蠢的 if 语句,无论我做什么,我都无法让 if 语句转到正确的标签。

  1. Hitting 'y' works, cmd-prompt stops at START DEPLOY
  2. BUT, if I type 'n' cmd prints START DEPLOY then goes to end, instead of going direct to the cancel label.
  1. 点击“y”有效,cmd-prompt 在 START DEPLOY 停止
  2. 但是,如果我输入 'n' cmd 打印 START DEPLOY 然后结束,而不是直接转到取消标签。

Can you help?

你能帮我吗?

:getConfirmation
set /p confirmDeploy =Confirm deployment of code [y/n] ?: 
if "%confirmDeploy%"=="y". goto deployCode
if "%confirmDeploy%"=="n". goto cancelDeploy

:deployCode
ECHO START DEPLOY
goto end

:cancelDeploy
ECHO DEPLOY CANCELLED
goto end

回答by Bali C

Try this:

尝试这个:

@echo off
:getConfirmation
set /p confirmDeploy=Confirm deployment of code [y/n] ?: 
if %confirmDeploy%==y goto :deployCode
if %confirmDeploy%==n goto :cancelDeploy

:deployCode
ECHO START DEPLOY
goto end

:cancelDeploy
ECHO DEPLOY CANCELLED
goto end

回答by Codemaster Bob

If we ignore the fact that required input was y. or n. (due to the . in the if comparison), nobody noticed the ACTUAL problem with c14kaa's script (except Nick DeVore but didn't say why). The original script had the line

如果我们忽略所需输入是 y 的事实。或 (由于 if 比较中的 .),没有人注意到 c14kaa 脚本的实际问题(Nick DeVore 除外,但没有说明原因)。原始脚本有一行

set /p confirmDeploy =Confirm deployment of code [y/n] ?:

set /p confirmDeploy =确认代码部署 [y/n] ?:

Nick mentioned that this did not put the response into the variable. That is because it was putting the response into "confirmDeploy " (the space is part of the variable name, just another foible of cmd's input parsing). Thus when c14kaa used %confirmDeploy%, it would have expanded to %confirmDeploy% (i.e. been taken literally) unless that variable had been set elsewhere. I can only assume that c14kaa had turned off echoing because the fact that confirmDeploy did not substitute (or contained something other than y or n) would have been a big clue. It would have also revealed the problem with the . in the if statement.

Nick 提到这并没有将响应放入变量中。那是因为它将响应放入“confirmDeploy”(空格是变量名的一部分,只是 cmd 输入解析的另一个弱点)。因此,当 c14kaa 使用 %confirmDeploy% 时,它会扩展为 %confirmDeploy%(即字面意思),除非该变量已在其他地方设置。我只能假设 c14kaa 已关闭回显,因为 confirmDeploy 没有替代(或包含除 y 或 n 以外的其他内容)这一事实将是一个重要线索。它也会揭示 . 在 if 语句中。

As for the other suggestions, having "" around the variable (and hence needed in the matching string) is preferred to stop syntax errors when the variable is blank (generating the statement 'if == y', what jeb means by "failing"), the : before the label name in the goto is ignored and there needs to be a space after the /I in John's version (even though the if command has only one option, some commands have many and they can be put together such as in "findstr /ivn ..." so the space marks the end of the list).

至于其他建议,当变量为空时(生成语句 'if == y',jeb 所说的“失败”是什么意思,最好在变量周围加上“”(因此需要在匹配字符串中)来阻止语法错误),忽略 goto 中标签名称之前的 : 并且在 John 的版本中 /I 之后需要有一个空格(即使 if 命令只有一个选项,但有些命令有很多并且可以放在一起,例如在“findstr /ivn ...”中,所以空格标志着列表的结尾)。

The only other comment I would make is that c14kaa assumes that the user will always enter the correct response (y or n) because otherwise the script will "fall through" into the :deployCode section (probably not the ideal default behaviour). This explains the response obtained to the suggestion by Matt (echo bad input). Since the response was being put into confirmDeploy<space> it meant that both tests (using confirmDeploy without the space) failed.

我要发表的唯一其他评论是 c14kaa 假设用户将始终输入正确的响应(y 或 n),否则脚本将“落入” :deployCode 部分(可能不是理想的默认行为)。这解释了对 Matt 建议的响应(回显错误输入)。由于响应被放入 confirmDeploy<space> 中,这意味着两个测试(使用没有空格的 confirmDeploy)都失败了。

Taking all of the above leaves us with Reny's version (with some explanation added).

综上所述,我们得到了 Reny 的版本(添加了一些解释)。

回答by Reny

You're on the right track, just needed to clean up syntax and spacing errors. This will work:

你在正确的轨道上,只需要清理语法和间距错误。这将起作用:

@echo off  
:getConfirmation  
set /p confirmDeploy=Confirm deployment of code [y/n] ?:   
if /I "%confirmDeploy%"=="y" goto deployCode  
if /I "%confirmDeploy%"=="n" goto cancelDeploy  
REM added goto getConfirmation in case of invalid responses  
goto getConfirmation

:deployCode  
ECHO START DEPLOY  
goto end  

:cancelDeploy  
ECHO DEPLOY CANCELLED  
goto end

回答by Mat

The problem is that neither of your tests work. You're checking for the user entering something like y.or n.(but I don't think you can actually enter anything that will match).

问题是你的测试都不起作用。您正在检查用户是否输入了类似y.或的内容n.(但我认为您实际上无法输入任何匹配的内容)。

Try with:

尝试:

if "%confirmDeploy%".=="y". goto deployCode
if "%confirmDeploy%".=="n". goto cancelDeploy
echo bad input
goto getConfirmation

...

回答by John A

try this too:

也试试这个:

@echo off
:getConfirmation
set /p confirmDeploy=Confirm deployment of code [y/n] ?: 
if /I%confirmDeploy%==y goto :deployCode
if /I%confirmDeploy%==n goto :cancelDeploy

:deployCode
ECHO START DEPLOY
goto end

:cancelDeploy
ECHO DEPLOY CANCELLED
goto end

The /I argument makes it case-insensitive.

/I 参数使其不区分大小写。