Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Прочее";
Текущий архив: 2013.03.22;
Скачать: [xml.tar.bz2];

Вниз

Странное поведение 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;
Скачать: [xml.tar.bz2];

Наверх




Память: 0.46 MB
Время: 0.153 c
15-1340134086
Artem
2012-06-19 23:28
2013.03.22
Посоветуйте надежный Android телефон


2-1331367948
niro---
2012-03-10 12:25
2013.03.22
Заставить работать TWebBrowser в потоке из DLL


15-1345822753
brother
2012-08-24 19:39
2013.03.22
Сис админ - диагноз?


15-1339444852
alexdn
2012-06-12 00:00
2013.03.22
PHPbb движёк


15-1341693003
Юрий
2012-07-08 00:30
2013.03.22
С днем рождения ! 8 июля 2012 воскресенье





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский