2011-06-27 134 views
6

我刚开始学习如何编写脚本。我想了解系统如何处理错误级别以及它们如何用于错误处理。我知道环境变量%ERRORLEVEL%和系统的错误级别有区别。如果我正确理解这一点,那么代码将在检查前一个命令的错误级别之前检查环境变量。因此,在我的程序中,我试图连接一个启动/停止脚本,它将启动/停止给定机器的所有脚本(用于测试,我只使用一个应用程序notepad.exe作为示例)。我有两个包装脚本,通过将参数传递给独立脚本来启动或停止应用程序。如果独立脚本中存在错误,它将使用命令 EXIT /B n批处理编程,错误处理和启动命令

命令来设置错误级别。一旦控制权返回给调用脚本,如果退出状态为非零,它将转到错误处理脚本。

首先,我手动将%ERRORLEVEL%设置为零,然后在START或TASKKILL命令之后测试错误。但后来我读了清除%ERRORLEVEL%与 SET ERRORLEVEL= 是一个更好的方法。我的问题进来时,我尝试每当我测试ERRORLEVEL该命令后,它始终是大于或等于1,除非我用SET ERRORLEVEL = 0 I运行启动命令之前

START "" notepad.exe 

启动应用程序。我在下面插入了四个脚本的代码。任何见解和建议将不胜感激。

appstart.bat:

@echo off 
:: Script for application Start 
set ERRORLEVEL= 
:: **** 
:: Additional Batch files will be executed from within this file 
:: Example: 
::  Call Appbat01.bat 
:: The called batch file should set ERRORLEVEL non-zero if error 
:: **** 

call test.bat -start 
if ERRORLEVEL 1 (call error.bat) 
echo. 
echo Control was returned to appstart.bat... 
:: **** End Calls 
goto end 

:end 

appstop.bat:

@echo off 
:: Script for application Start 
set ERRORLEVEL= 
:: **** 
:: Additional Batch files will be executed from within this file 
:: Example: 
::  Call Appbat01.ba 
:: The called batch file should set ERRORLEVEL non-zero if error 
:: **** 

call test.bat -stop 
if ERRORLEVEL 1 (call error.bat) 
echo. 
echo Control was returned to appstop.bat... 
:: **** End Calls 
goto end 

:end 

test.bat的:

@echo off 
if "%1"=="-start" goto :start 
if "%1"=="-stop" goto :stop 
goto wrongParams 

:start 
::**** 
:: Insert start up stripts here... 
:: If there is an error, set ERRORLEVEL=1 
::**** 
    set ERRORLEVEL=0 
    echo. 
    echo ******** 
    echo starting the service... 
    echo. 
    ::start "" "C:\Program Files\Microsoft Office\office11\winword.exe" 
    start notepad.exe 
    if ERRORLEVEL 1 goto error 
    qprocess notepad.exe 
    echo *Start.success* ERRORLEVEL is: %ERRORLEVEL% 
    echo. 
    goto end 

:stop 
::**** 
:: Insert stopping stripts here... 
:: If there is an error, set ERRORLEVEL>1 
::**** 
    set ERRORLEVEL=0 
    echo. 
    echo ******** 
    echo stopping the service... 
    echo. 
    qprocess notepad.exe 
    taskkill /f /im notepad.exe 
    if ERRORLEVEL 1 goto noProcess 
    goto end 

:noProcess 
    set ERRORLEVEL=2 
    echo *noProcess* ERRORLEVEL is now: %ERRORLEVEL% 
    echo. 
    exit /b 2 
:error 
:: Errorhandler. Log application status and cause of error here. Set 
:: ERRORLEVEL > 1 before returning to caller. 
    set ERRORLEVEL=1 
    echo. 
    echo **** Error handler inside test.bat **** 
    echo. 
    echo *error* ERRORLEVEL is now: %ERRORLEVEL% 
    echo. 
    exit /b 1 

:wrongParams 
:: Output an error if the wrong parameters were passed to this script. 
:: Maybe try to self correct the parameter... 
    set ERRORLEVEL=1 
    echo. 
    echo '%1' is an invalid parameter. 
    echo Usage: %0 [-stop ^| -start] 
    echo *wrongParams* ERRORLEVEL is now: %ERRORLEVEL% 
    echo. 
    exit /b 1 
:end 

error.bat:

@echo off 
echo **** You have reached error.bat **** 
echo ERRORLEVEL inside of error.bat is: %ERRORLEVEL% 
echo. 
::*** Handle error...*** 
goto error%ERRORLEVEL% 

:error2 
    echo The process could not be stopped for some reason. 
    goto end 
:error1 
    echo The process had an error in start up. 
::***    *** 
    goto end 

:end 
+0

如果您正在运行代码,这是我遇到错误的地方。如果我使用启动,它会启动记事本,没有任何错误。如果我使用stop,它会停止记事本而没有任何错误。如果我再次使用stop,它会按预期抛出错误。现在,如果我使用启动,它应该再次启动记事本没有任何错误。但是,START命令之后的if语句为true,并且发生错误。我不知道为什么会发生这种情况。请帮忙! – grocky

回答

12

你不应该设置%errorlevel%变量。你是正确的,有区别;从退出进程获得的错误级别是一个内部寄存器,您可以使用%errorlevel%语法读取该错误级别。但是,如果您创建一个名为ERRORLEVEL的变量,它将屏蔽内部寄存器,并且无法访问退出代码。

如果您需要将ERRORLEVEL寄存器设置为特定值时,你可以用下面的命令做到这一点:

%comspec% /c exit %value% 

这将产生一个过程,立即与所需的代码退出。

+0

太棒了!我已经能够运行脚本而没有任何意外的错误。我试过之前没有直接设置ERRORLEVEL,但在运行下一个命令之前我无法重置它。但是现在,在完成错误处理后,使用error.bat脚本中给出的命令将ERRORLEVEL设置回零。 – grocky