Главная страница
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.07 c
15-1351110603
Юрий
2012-10-25 00:30
2013.03.22
С днем рождения ! 25 октября 2012 четверг


15-1340803269
AV
2012-06-27 17:21
2013.03.22
А вы сегодня как накосячили на свою голову? :)


15-1341912377
Омлет
2012-07-10 13:26
2013.03.22
Клонирование объектов


2-1336838970
АлексеЕей
2012-05-12 20:09
2013.03.22
Задача по информатике


15-1350728255
NailMan
2012-10-20 14:17
2013.03.22
Медицинские заморочечки