Windows批处理文件中的If语句

时间:2022-09-15 19:16:49

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. 点击“y”的工作,在开始部署时就会停止。
  3. BUT, if I type 'n' cmd prints START DEPLOY then goes to end, instead of going direct to the cancel label.
  4. 但是,如果我输入'n' cmd打印开始部署,那么就结束了,而不是直接去取消标签。

Can you help?

你能帮助吗?

:getConfirmationset /p confirmDeploy =Confirm deployment of code [y/n] ?: if "%confirmDeploy%"=="y". goto deployCodeif "%confirmDeploy%"=="n". goto cancelDeploy:deployCodeECHO START DEPLOYgoto end:cancelDeployECHO DEPLOY CANCELLEDgoto end

5 个解决方案

#1


16  

Try this:

试试这个:

@echo off:getConfirmationset /p confirmDeploy=Confirm deployment of code [y/n] ?: if %confirmDeploy%==y goto :deployCodeif %confirmDeploy%==n goto :cancelDeploy:deployCodeECHO START DEPLOYgoto end:cancelDeployECHO DEPLOY CANCELLEDgoto end

#2


3  

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或n。在if比较中,没有人注意到c14kaa脚本的实际问题(除了Nick DeVore,但没有说明原因)。原来的剧本有台词。

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

set /p确认部署=确认代码部署[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已经关闭了echo,因为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表示“失败”),在变量为空时(生成语句'if = y', jeb表示“失败”)时,更倾向于停止语法错误:goto标签名称之前被忽略后,需要有一个空间/我在约翰的版本(尽管如果命令只有一个选项,一些命令有许多可以放在一起,他们如在“中/ 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 ,这意味着两个测试(使用没有空格的confirmDeploy)都失败了。

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

把上面所有的内容都留给我们,用Reny的版本(加上一些解释)。

#3


1  

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

#4


0  

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 deployCodeif "%confirmDeploy%".=="n". goto cancelDeployecho bad inputgoto getConfirmation...

#5


0  

try this too:

试试这个:

@echo off:getConfirmationset /p confirmDeploy=Confirm deployment of code [y/n] ?: if /I%confirmDeploy%==y goto :deployCodeif /I%confirmDeploy%==n goto :cancelDeploy:deployCodeECHO START DEPLOYgoto end:cancelDeployECHO DEPLOY CANCELLEDgoto end

The /I argument makes it case-insensitive.

我的论点使它不区分大小写。

#1


16  

Try this:

试试这个:

@echo off:getConfirmationset /p confirmDeploy=Confirm deployment of code [y/n] ?: if %confirmDeploy%==y goto :deployCodeif %confirmDeploy%==n goto :cancelDeploy:deployCodeECHO START DEPLOYgoto end:cancelDeployECHO DEPLOY CANCELLEDgoto end

#2


3  

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或n。在if比较中,没有人注意到c14kaa脚本的实际问题(除了Nick DeVore,但没有说明原因)。原来的剧本有台词。

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

set /p确认部署=确认代码部署[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已经关闭了echo,因为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表示“失败”),在变量为空时(生成语句'if = y', jeb表示“失败”)时,更倾向于停止语法错误:goto标签名称之前被忽略后,需要有一个空间/我在约翰的版本(尽管如果命令只有一个选项,一些命令有许多可以放在一起,他们如在“中/ 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 ,这意味着两个测试(使用没有空格的confirmDeploy)都失败了。

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

把上面所有的内容都留给我们,用Reny的版本(加上一些解释)。

#3


1  

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

#4


0  

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 deployCodeif "%confirmDeploy%".=="n". goto cancelDeployecho bad inputgoto getConfirmation...

#5


0  

try this too:

试试这个:

@echo off:getConfirmationset /p confirmDeploy=Confirm deployment of code [y/n] ?: if /I%confirmDeploy%==y goto :deployCodeif /I%confirmDeploy%==n goto :cancelDeploy:deployCodeECHO START DEPLOYgoto end:cancelDeployECHO DEPLOY CANCELLEDgoto end

The /I argument makes it case-insensitive.

我的论点使它不区分大小写。