Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statement in Windows Batch file

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

  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.

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
like image 395
c14kaa Avatar asked Dec 01 '22 06:12

c14kaa


2 Answers

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
like image 138
Bali C Avatar answered Dec 06 '22 08:12

Bali C


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

set /p confirmDeploy =Confirm deployment of code [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.

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).

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.

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

like image 37
Codemaster Bob Avatar answered Dec 06 '22 08:12

Codemaster Bob