【发布时间】:2011-06-27 20:20:04
【问题描述】:
我刚刚开始学习如何编写脚本。我试图了解系统如何处理错误级别以及如何在错误处理中使用它们。我知道环境变量 %ERRORLEVEL% 和系统的错误级别之间存在差异。如果我理解正确,那么
If ERRORLEVEL 1
代码会在检查前一个命令的错误级别之前检查环境变量。
因此,在我的程序中,我试图连接一个启动/停止脚本,该脚本将启动/停止给定机器的所有脚本(为了测试,我只是使用一个应用程序 notepad.exe 作为示例)。我有两个包装脚本,它们可以通过将参数传递给独立脚本来启动或停止应用程序。如果独立脚本中有错误,它将使用
EXIT /B n
命令。一旦控制权返回给调用脚本,如果退出状态非零,它将转到错误处理脚本。
起初我手动将 %ERRORLEVEL% 设置为零,然后在 START 或 TASKKILL 命令之后测试错误。但后来我读到了清除 %ERRORLEVEL% 的内容
SET ERRORLEVEL=
是一种更好的方法。当我尝试使用启动应用程序时出现问题
START "" notepad.exe
每当我在此命令之后测试错误级别时,它总是大于或等于 1,除非我在运行启动命令之前使用 SET ERRORLEVEL=0。我已经为下面的四个脚本插入了代码。任何见解和建议将不胜感激。
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
【问题讨论】:
-
如果您正在运行代码,这就是我得到错误的地方。如果我使用 start,它会很好地启动记事本而不会出现任何错误。如果我使用停止,它会停止记事本而不会出现任何错误。如果我再次使用 stop ,它会按预期抛出错误。现在,如果我使用 start,它应该会再次启动记事本而不会出现任何错误。但是,在 START 命令之后的 if 语句为真,它会出错。我不确定为什么会这样。请帮忙!