我刚开始学习如何编写脚本。我试图了解系统如何处理错误级别以及如何在错误处理中使用它们。我知道环境变量%ERRORLEVEL%与系统的错误级别之间存在差异。如果我理解正确,那么
If ERRORLEVEL 1
代码将在检查上一个命令的错误级别之前检查环境变量。
所以,在我的程序中,我试图连接一个启动/停止脚本,它将启动/停止给定机器的所有脚本(用于测试我只是使用一个应用程序notepad.exe作为示例)。我有两个包装脚本,可以通过将参数传递给独立脚本来启动或停止应用程序。如果独立脚本中存在错误,它将使用以下命令设置errorlevel
EXIT /B n
命令。一旦控制返回到调用脚本,如果退出状态为非零,它将转到错误处理脚本。
首先,我手动将%ERRORLEVEL%设置为零,然后在START或TASKKILL命令后测试错误。但后来我读到了清除%ERRORLEVEL%
SET ERRORLEVEL=
是一种更好的方法。当我尝试使用
START "" notepad.exe
每当我在此命令之后测试errorlevel时,除非在运行start命令之前使用SET ERRORLEVEL = 0,否则它总是大于或等于1。我已经为下面的四个脚本插入了代码。任何见解和建议将不胜感激。
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 :(得分:12)
你永远不应该设置%errorlevel%变量。你是对的,存在差异;从现有进程获得的错误级别是一个内部寄存器,您可以使用%errorlevel%语法读取该错误级别。但是,如果创建名为ERRORLEVEL的变量,它将屏蔽内部寄存器,并且您将无法访问退出代码。
如果需要将errorlevel寄存器设置为特定值,可以使用以下命令执行此操作:
%comspec% /c exit %value%
这将产生一个立即退出所需代码的进程。