2012-06-18 2 views
4

나는 이런 식으로 뭔가 할 : 나는 "재생"옵션을 선택하면선택과 오류 수준?

echo 1-exit 
echo 2-about 
echo 3-play 
choice /c 123 >nul 
if errorlevel 1 goto exit 
if errorlevel 2 goto about 
if errorlevel 3 goto play 
:play 
blah 
:about 
blah 
:exit 
cls 

를,이 종료됩니다. 어떻게 이런 일이 일어나지 않도록합니까?

답변

6

if errorlevel 표현식이 반환 한 실제 오류 수준이 지정된 값보다 크거나 같으면 표현식이 true로 평가됩니다. 따라서 3을 치면 첫 번째 if 표현식이 true이고 스크립트가 종료됩니다. 자세한 내용은 help if으로 문의하십시오.

두 가지 간단한 해결 방법이 있습니다.

처음 하나 (이상) - 주어진 값 %ERRORLEVEL% 시스템 변수의 실제을 비교 한 함께 if errorlevel 식 교체 - comparisions의 변경 순서

if "%ERRORLEVEL%" == "1" goto exit 
if "%ERRORLEVEL%" == "2" goto about 
if "%ERRORLEVEL%" == "3" goto play 

번째 :

if errorlevel 3 goto play 
if errorlevel 2 goto about 
if errorlevel 1 goto exit 
1

쉬운 방법 이 문제를 해결하려면 % errorlevel % 값을 사용하여 원하는 레이블로 직접 이동하십시오 :

echo 1-exit 
echo 2-about 
echo 3-play 
choice /c 123 >nul 
goto option-%errorlevel% 
:option-1 
rem play 
blah 
:option-2 
rem about 
blah 
:option-3 
exit 
cls 
관련 문제