Главная страница
    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.071 c
2-1342772214
Scott Storch
2012-07-20 12:16
2013.03.22
Завершить работу приложения


2-1328028334
alexdn
2012-01-31 20:45
2013.03.22
Canvas на Image


15-1332156687
alexdn
2012-03-19 15:31
2013.03.22
Фотошоп?..


15-1336640074
jacksotnik
2012-05-10 12:54
2013.03.22
Можно ли в DesignMode открыть закрыть модуль зная его имя?


2-1333345551
TSubject
2012-04-02 09:45
2013.03.22
Вопрос по выпадающему списку





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
Английский Французский Немецкий Итальянский Португальский Русский Испанский