Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2013.03.22;
Скачать: CL | DM;

Вниз

Странное поведение CHOICE (batch file)   Найти похожие ветки 

 
Lamer@fools.ua ©   (2012-02-14 22:41) [0]

1.cmd:
@echo off

if 1 equ 1 (
   choice /c 12 /m "Press 1 to continue; 2 to cancel" /t 30 /d 2
   echo A: %ERRORLEVEL%
   if %ERRORLEVEL% neq 1 (
       echo * Cancelled.
   )
)

2.cmd:
@echo off

REM if 1 equ 1 (
   choice /c 12 /m "Press 1 to continue; 2 to cancel" /t 30 /d 2
   echo A: %ERRORLEVEL%
   if %ERRORLEVEL% neq 1 (
       echo * Cancelled.
   )
REM )

Win 7 x64
Кто виноват и что делать?


 
Lamer@fools.ua ©   (2012-02-14 22:42) [1]

В варианте 1 (со вложенным IF-ом), ERRORLEVEL всегда 0.


 
Lamer@fools.ua ©   (2012-02-14 22:45) [2]

Сам спросил. Сам ответил. ERRORLEVEL принимает правильное значение при выходе из IF-а. Всегда приятно поговорить с умным собеседником :)

P.S. Но всё равно кто-то виноват. Надо разобраться как следует и наказать кого попало.


 
Lamer@fools.ua ©   (2012-02-14 22:50) [3]

На всякий случай напишу workaround:

if 1 neq 1 goto SKIP_CHOICE
choice /c 12 /m "Press 1 to continue; 2 to cancel" /t 30 /d 2
echo A: %ERRORLEVEL%
if %ERRORLEVEL% neq 1 (
   echo * Cancelled.
)
:SKIP_CHOICE


 
Lamer@fools.ua ©   (2012-03-02 22:06) [4]

Случайно нашел описание этой проблемы в справке по команде SET:

Finally, support for delayed environment variable expansion has been
added.  This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE.  See CMD /?

Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed.  The following example
demonstrates the problem with immediate variable expansion:

   set VAR=before
   if "%VAR%" == "before" (
       set VAR=after
       if "%VAR%" == "after" @echo If you see this, it worked
   )

would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement.  So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal.  Similarly, the following example
will not work as expected:

   set LIST=
   for %i in (*) do set LIST=%LIST% %i
   echo %LIST%

in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:

   for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time.  If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:

   set VAR=before
   if "%VAR%" == "before" (
       set VAR=after
       if "!VAR!" == "after" @echo If you see this, it worked
   )

   set LIST=
   for %i in (*) do set LIST=!LIST! %i
   echo %LIST%



Страницы: 1 вся ветка

Текущий архив: 2013.03.22;
Скачать: CL | DM;

Наверх




Память: 0.48 MB
Время: 0.049 c
15-1351684728
Inovet
2012-10-31 15:58
2013.03.22
И Яндекс не остался в стороне от браузеростроения


15-1351850650
AV
2012-11-02 14:04
2013.03.22
Нейронные сети распознавания изображения. Для идиотов :)


2-1339949716
Cobalt
2012-06-17 20:15
2013.03.22
Вывод текста


2-1338620541
novichek
2012-06-02 11:02
2013.03.22
FireMonkey - Binding


15-1351234770
AV
2012-10-26 10:59
2013.03.22
Легкая пятничная. Не гуглить! :)