Главная страница
    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.073 c
15-1343498887
stas
2012-07-28 22:08
2013.03.22
отправить факс через VentaFax (из моей программы)


2-1333010150
rodionov_uv
2012-03-29 12:35
2013.03.22
Помогите организовать поиск и удаление строки в Excele


2-1334761862
Afrost
2012-04-18 19:11
2013.03.22
разделение массива на несколько других массив


15-1329078602
Юрий
2012-02-13 00:30
2013.03.22
С днем рождения ! 13 февраля 2012 понедельник


15-1328563233
Германн
2012-02-07 01:20
2013.03.22
Небольшой опрос по провайдерам и SMTP-серверам





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