Expandindo a ótima resposta do grawity:
Primeiro, responda sua pergunta:
Por que o script em lote termina quando% CHECKCONTINUE% recebe um valor nulo?
O problema é que, na linha 16, você faz isso:
if %CHECKCONTINUE%==n GOTO exit
Como CHECKCONTINUE
é "indefinido", ele é avaliado como uma string "vazia", portanto, a instrução na linha 16 está realmente fazendo:
if ==n GOTO exit
Esta é uma declaração inválida porque não há nada no lado esquerdo do "=="
. Então, o script em lote termina quando ele tenta executar uma instrução formatada incorretamente:
C:\>script.cmd
I'm about to...
1.) Remove the registry data that specifies settings for TF2
2.) Forcibly disable Steam Cloud.
Okay to continue? (y/n): <ENTER key pressed>
GOTO was unexpected at this time.
C:\>
Você teria um problema semelhante se alguém digitar algo que contenha um espaço:
C:\>script.cmd
I'm about to...
1.) Remove the registry data that specifies settings for TF2
2.) Forcibly disable Steam Cloud.
Okay to continue? (y/n): Yes please
please==n was unexpected at this time.
C:\>
Para corrigir isso, você deve usar aspas duplas em torno dos termos assim:
if "%CHECKCONTINUE%"=="n" GOTO :exit
Isso é necessário se as variáveis usadas puderem estar "vazias" ou se elas puderem ter espaço em branco incorporado, mas é sempre uma boa ideia usar sempre aspas duplas ao avaliar com "=="
.
Nota: Alguns erros (como o acima com "if"
e "=="
são erros "fatais" que farão com que a execução do script em lote pare imediatamente. Outros erros (como o abaixo com "set"
), são erros "não fatais" Para erros "não fatais", a instrução com o erro NÃO é executada, uma mensagem de erro é mostrada e o script em lote continua a ser executado a partir da próxima instrução
Em seguida, como a questão de grawity apontou para esta linha:
set %CHECKCONTINUE%=
Isso não modifica CHECKCONTINUE, mas usa seu valor como o nome da variável.
Novamente, se CHECKCONTINUE
for "indefinido", ele será avaliado como uma string "vazia", portanto, a instrução está realmente fazendo:
set =
Esta também é uma instrução inválida porque não há nada do lado esquerdo do "="
.
E estas linhas:
if defined %CHECKCONTINUE% GOTO loop-notvalid
if not defined %CHECKCONTINUE% GOTO loop-noreply
"if defined"
(e "if not defined"
) espera um nome de variável, não o valor de uma variável. Se CHECKCONTINUE
fosse indefinido, %CHECKCONTINUE%
avaliaria uma string vazia, e essas declarações seriam realmente:
if defined GOTO loop-notvalid
if not defined GOTO loop-noreply
Aqui, "if defined"
(e "if not defined"
) vai verificar se uma variável chamada GOTO
está definida ou não.
Além disso, para essas três linhas, se CHECKCONTINUE
estivesse realmente definido, "set"
e "if defined"
operariam na "value"
da variável, em vez da "name"
da própria variável. Então, se CHECKCONTINUE
já tiver um valor de "y"
, então:
set %CHECKCONTINUE%=
if defined %CHECKCONTINUE% goto loop-notvalid
if not defined %CHECKCONTINUE% goto loop-noreply
seria realmente visto como:
set y=
if defined y goto loop-notvalid
if not defined y goto loop-noreply
Exemplo "script.cmd":
@set "CHECKCONTINUE="
@rem ## CHECKCONTINUE="%CHECKCONTINUE%" (undefined/empty).
@rem ## 05: set %CHECKCONTINUE%=
set %CHECKCONTINUE%=
@echo This doesn't set the value of of the variable named "CHECKCONTINUE".
@echo Since no variable name is actually specified, it is an error.
@set "CHECKCONTINUE=yes"
@set "yes=something"
@rem ## CHECKCONTINUE="%CHECKCONTINUE%" and the value of the variable named "yes"="%yes%"
@rem ## 17: set %CHECKCONTINUE%=
set %CHECKCONTINUE%=
@echo This doesn't set the value of the variable named "CHECKCONTINUE".
@echo Since CHECKCONTINUE="%CHECKCONTINUE%", it sets the value of the variable named
@echo "%CHECKCONTINUE%". No error is shown because the statement is valid.
@echo It could have been a problem (well, at least a big annoyance) if
@echo CHECKCONTINUE had the value: "path". The statement
@echo should be: set "CHECKCONTINUE="
@rem ## 27: echo CHECKCONTINUE still has the value: "%CHECKCONTINUE%"
@echo CHECKCONTINUE still has the value: "%CHECKCONTINUE%"
@rem ## 30: echo and the variable named "%CHECKCONTINUE%" is now empty="%yes%"
@echo and the variable named "%CHECKCONTINUE%" is now empty="%yes%"
@set "yes="
@set "CHECKCONTINUE="
@set "echo=something"
@rem ## CHECKCONTINUE="%CHECKCONTINUE%" (undefined) and the value of the variable
@rem ## named "echo"="%echo%".
@rem ## 41: if defined %CHECKCONTINUE% echo Variable is defined.
if defined %CHECKCONTINUE% echo Variable is defined.
@echo This doesn't check if the variable named "CHECKCONTINUE" is defined.
@echo Since it's "empty", it is skipped (well, there is nothing there to
@echo "skip") and "if defined" is checking the next word (which is "echo").
@echo What's left is: if defined echo Variable is defined.
@echo So, it checks if a variable named "echo" is defined (which it is).
@echo Since "if defined" has checked a variable named "echo", it then tries
@echo to execute the rest of the line starting with the word "Variable",
@echo as a command. This fails and is an error. The statement
@echo should be: if defined CHECKCONTINUE echo Variable is defined.
@set "echo="
@rem ## CHECKCONTINUE="%CHECKCONTINUE%" (undefined) and "echo"="%echo%" (undefined).
@rem ## 59: if not defined %CHECKCONTINUE% echo The-variable-is-not-defined.
if not defined %CHECKCONTINUE% echo The-variable-is-not-defined.
@echo Similar: Since "if not defined" has checked a variable named "echo"
@echo (which is "undefined"), it then tries to execute the rest of the
@echo line: "The-variable-is-not-defined." as a command. This fails and is
@echo an error. The statement
@echo should be: if not defined CHECKCONTINUE echo The-variable-is-not-defined.
@set "echo=something"
@rem ## CHECKCONTINUE="%CHECKCONTINUE%" (undefined) and "echo"="%echo%".
@rem ## 73: if defined %CHECKCONTINUE% echo Verify this.
if defined %CHECKCONTINUE% echo Verify this.
@echo Again, similar: Since "if defined" has checked a variable named
@echo "echo", it then tries to execute the rest of the line starting with
@echo the word: "Verify" as a command. This happens to be a valid command
@echo but it also fails because of an incorrect parameter for the command.
@echo The statement should be: if defined CHECKCONTINUE echo Verify this.
@set "echo="
@set "CHECKCONTINUE=yes"
@set "yes="
@rem ## CHECKCONTINUE="%CHECKCONTINUE%" and the variable named "yes"="%yes%" (undefined).
@rem ## 90: if not defined %CHECKCONTINUE% echo CHECKCONTINUE is not defined.
if not defined %CHECKCONTINUE% echo CHECKCONTINUE is not defined.
@echo Here "CHECKCONTINUE" is defined, but "if not defined" still doesn't
@echo check if the variable named "CHECKCONTINUE" is defined. Since
@echo CHECKCONTINUE has a value of "%CHECKCONTINUE%", "if not defined" is
@echo checking if a variable named "%CHECKCONTINUE%" is defined (which it isn't).
@echo This causes "if not defined" to proceed and echo the message when
@echo that's probably not what was intended. The statement
@echo should be: if not defined CHECKCONTINUE echo CHECKCONTINUE is not defined.
Rodando "script.cmd" você obteria:
## CHECKCONTINUE="" (undefined/empty).
## 05: set %CHECKCONTINUE%=
C:\>set =
The syntax of the command is incorrect.
This doesn't set the value of of the variable named "CHECKCONTINUE".
Since no variable name is actually specified, it is an error.
## CHECKCONTINUE="yes" and the value of the variable named "yes"="something"
## 17: set %CHECKCONTINUE%=
C:\>set yes=
This doesn't set the value of the variable named "CHECKCONTINUE".
Since CHECKCONTINUE="yes", it sets the value of the variable named
"yes". No error is shown because the statement is valid.
It could have been a problem (well, at least a big annoyance) if
CHECKCONTINUE had the value: "path". The statement
should be: set "CHECKCONTINUE="
## 27: echo CHECKCONTINUE still has the value: "%CHECKCONTINUE%"
CHECKCONTINUE still has the value: "yes"
## 30: echo and the variable named "yes" is now empty="%yes%"
and the variable named "yes" is now empty=""
## CHECKCONTINUE="" (undefined) and the value of the variable
## named "echo"="something".
## 41: if defined %CHECKCONTINUE% echo Variable is defined.
C:\>if defined echo Variable is defined.
'Variable' is not recognized as an internal or external command,
operable program or batch file.
This doesn't check if the variable named "CHECKCONTINUE" is defined.
Since it's "empty", it is skipped (well, there is nothing there to
"skip") and "if defined" is checking the next word (which is "echo").
What's left is: if defined echo Variable is defined.
So, it checks if a variable named "echo" is defined (which it is).
Since "if defined" has checked a variable named "echo", it then tries
to execute the rest of the line starting with the word "Variable",
as a command. This fails and is an error. The statement
should be: if defined CHECKCONTINUE echo Variable is defined.
## CHECKCONTINUE="" (undefined) and "echo"="" (undefined).
## 59: if not defined %CHECKCONTINUE% echo The-variable-is-not-defined.
C:\>if not defined echo The-variable-is-not-defined.
'The-variable-is-not-defined.' is not recognized as an internal or external command,
operable program or batch file.
Similar: Since "if not defined" has checked a variable named "echo"
(which is "undefined"), it then tries to execute the rest of the
line: "The-variable-is-not-defined." as a command. This fails and is
an error. The statement
should be: if not defined CHECKCONTINUE echo The-variable-is-not-defined.
## CHECKCONTINUE="" (undefined) and "echo"="something".
## 73: if defined %CHECKCONTINUE% echo Verify this.
C:\>if defined echo Verify this.
An incorrect parameter was
entered for the command.
Again, similar: Since "if defined" has checked a variable named
"echo", it then tries to execute the rest of the line starting with
the word: "Verify" as a command. This happens to be a valid command
but it also fails because of an incorrect parameter for the command.
The statement should be: if defined CHECKCONTINUE echo Verify this.
## CHECKCONTINUE="yes" and the variable named "yes"="" (undefined).
## 90: if not defined %CHECKCONTINUE% echo CHECKCONTINUE is not defined.
C:\>if not defined yes echo CHECKCONTINUE is not defined.
CHECKCONTINUE is not defined.
Here "CHECKCONTINUE" is defined, but "if not defined" still doesn't
check if the variable named "CHECKCONTINUE" is defined. Since
CHECKCONTINUE has a value of "yes", "if not defined" is
checking if a variable named "yes" is defined (which it isn't).
This causes "if not defined" to proceed and echo the message when
that's probably not what was intended. The statement
should be: if not defined CHECKCONTINUE echo CHECKCONTINUE is not defined.
Além disso, como alternativa a "set /p"
, você pode usar "choice"
:
@echo off
title Registry restore script
rem Restores registry settings and disables the cloud
rem "quotes" around variable name and value for set visibly shows what
rem the variable is being set to and prevents accidentally including
rem trailing whitespace in the variable's value.
set "CHECKCONTINUE="
:listaction
echo I'm about to...
echo 1.) Remove the registry data that specifies settings for TF2
echo 2.) Forcibly disable Steam Cloud.
echo.
choice /c yn /M "Okay to continue"
set "CHECKCONTINUE=%errorlevel%"
if %CHECKCONTINUE% EQU 1 @echo Pressed Y && goto :start
if %CHECKCONTINUE% EQU 2 @echo Pressed N && goto :exit
if %CHECKCONTINUE% EQU 0 @echo Pressed Ctrl-C+n
@echo.
@echo Terminate batch job cancelled. You must enter a reply. Press n to exit.
@echo.
goto :listaction
rem The remainder of your code goes here ...
Observação: o código no rótulo: "loop-notvalid"
não é necessário porque "escolha" não aceitará respostas indefinidas (y / n).
Além disso, a única maneira de obter uma resposta "vazia" do commnad "choice" é se o usuário pressionar "Ctrl-C" para encerrar o trabalho em lotes e, em seguida, digitar N (No) no trabalho de lote "Terminate". (S / N)? prompt, indicando que eles não querem sair. O código acima captura isso e imprime uma mensagem, em seguida, salta (goto) para o rótulo ": listaction" para re-solicitar o usuário, assim você não precisa do código no rótulo "loop-noreply".
Não há necessidade de "redefinir" errorlevel, pois o comando choice cuida disso. E não é necessário limpar a variável CHECKCONTINUE
, porque ela sempre é definida como igual a %errorlevel%
antes que o valor de CHECKCONTINUE
seja examinado.
Por padrão, a escolha é "insensível a maiúsculas e minúsculas", então pressionar "Y" ou "N" é o mesmo que pressionar "y" ou "n". Esse comportamento pode ser alterado especificando /cs
na linha de comando de escolha.