批处理编程,错误处理,并启动命令 [英] Batch Programming, Error Handling, and Start Command

查看:472
本文介绍了批处理编程,错误处理,并启动命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习如何编写脚本。我想了解系统如何处理错误级别以及如何在错误处理中使用。我知道有环境变量%ERRORLEVEL%和系统的错误级别之间的差异。如果我正确的话,明白这一点
如果ERRORLEVEL 1
code会检查环境变量它检查previous命令的错误前水平。

所以,在我的计划,我想接口的启动/停止脚本,将启动/停止特定机器​​的所有脚本(用于测试我只使用一个应用程序的notepad.exe为例)。我有两个包装脚本,要么启动或通过传递参数给独立脚本停止应用程序。如果在独立的脚本的错误,它将使用设置错误级别
EXIT / Bñ

命令。一旦控制返回到调用脚本,它会去到一个错误处理脚本,如果退出状态是非零。

起初我设置%ERRORLEVEL%至零START或TASKKILL命令后一个错误手动,然后再测试。但后来我读到清除%ERRORLEVEL%以
SET ERRORLEVEL =
是一个更好的方法。我的问题进来时,我尝试启动应用程序

 开始→NOTEPAD.EXE

每当我此命令后,测试ERRORLEVEL它总是大于或等于1,除非我使用SET ERRORLEVEL = 0 I运行start命令。我插入了code下列四脚本。任何见解和建议将不胜AP preciated。

appstart.bat:

 关闭@echo
::脚本应用程序启动
设置ERRORLEVEL =
:: ****
::其他批处理文件会从该文件中执行
::例子:
::呼叫Appbat01.bat
::被调用批处理文件应该设置ERRORLEVEL非零值,如果错误
:: ****调用test.bat的-start
如果ERRORLEVEL 1(电话error.bat)
回声。
回声控制归还给appstart.bat ...
:: ****结束通话
转到结束:结束

appstop.bat:

 关闭@echo
::脚本应用程序启动
设置ERRORLEVEL =
:: ****
::其他批处理文件会从该文件中执行
::例子:
::呼叫Appbat01.ba
::被调用批处理文件应该设置ERRORLEVEL非零值,如果错误
:: ****调用test.bat的-stop
如果ERRORLEVEL 1(电话error.bat)
回声。
回声控制归还给appstop.bat ...
:: ****结束通话
转到结束:结束

test.bat的:

 关闭@echo
如果%1== - 启动转到:启动
如果%1== - 停后藤:停止
转到wrongParams:开始
:: ****
::这里插入启动stripts ...
::如果有错误,错误级别设置= 1
:: ****
    设置ERRORLEVEL = 0
    回声。
    回声********
    呼应启动服务...
    回声。
    ::开始,C:\\ Program Files文件\\的Microsoft Office \\ OFFICE11 \\ WINWORD.EXE
    启动NOTEPAD.EXE
    如果ERRORLEVEL 1页转到错误
    另外,QProcess NOTEPAD.EXE
    回声* Start.success * ERRORLEVEL是:%ERRORLEVEL%
    回声。
    转到结束:停止
:: ****
::将停止stripts这里...
::如果有错误,错误级别设置大于1
:: ****
    设置ERRORLEVEL = 0
    回声。
    回声********
    呼应停止服务...
    回声。
    另外,QProcess NOTEPAD.EXE
    TASKKILL / F / IM NOTEPAD.EXE
    如果ERRORLEVEL 1页转到noProcess
    转到结束:noProcess
    设置ERRORLEVEL = 2
    回声* noProcess * ERRORLEVEL现在是:%ERRORLEVEL%
    回声。
    出口/ B 2
:错误
::的ErrorHandler。登录应用程序状态和错误的原因在这里。组
:: ERRORLEVEL> 1日前返回到调用者。
    设置ERRORLEVEL = 1
    回声。
    ****呼应的test.bat里面的错误处理程序****
    回声。
    回声* *错误ERRORLEVEL现在是:%ERRORLEVEL%
    回声。
    退出/ B 1:wrongParams
::输出如果错误的参数传递给此脚本错误。
::也许尝试自行更正参数...
    设置ERRORLEVEL = 1
    回声。
    回声%1是无效的参数。
    回声用法:%0 -stop ^ | -开始]
    回声* wrongParams * ERRORLEVEL现在是:%ERRORLEVEL%
    回声。
    退出/ B 1
:结束

error.bat:

 关闭@echo
回声****您已达到error.bat ****
回声ERRORLEVEL内error.bat的是:%ERRORLEVEL%
回声。
:: ***处理错误... ***
转到错误%ERRORLEVEL%:误差2
    回声进程无法停止的某些原因。
    转到结束
:ERROR1
    回声进程曾在启动时出现错误。
:: *** ***
    转到结束:结束


解决方案

您应该永远不能设置%ERRORLEVEL%变量。你是正确的,是有区别的;您从退出的过程中得到的错误级别是可以用%ERRORLEVEL%语法读取内部寄存器。但是,如果您创建一个名为ERRORLEVEL变量,它会掩盖内部寄存器,你失去进入退出codeS。

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

 %COMSPEC%/ C出口百分比值%

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

I am just starting to learn how to script. I'm trying to understand how the system handles Error Levels and how they can be used in error handling. I know there is a difference between the environment variable %ERRORLEVEL% and the Error Level of the system. If I understand this correctly, then the If ERRORLEVEL 1 code would check the environment variable before it checks the error level of the previous command.

So, in my program I am trying to interface a startup/stop script that will start/stop all scripts of a given machine (for testing I'm just using one application notepad.exe as an example). I have two wrapper scripts that will either start up or stop the applications by passing arguments to the independent script. If there is an error in the independent script, it will set the errorlevel using the EXIT /B n

command. Once control is returned to the calling script, it will go to an error handling script if the exit status is non-zero.

At first I was setting the %ERRORLEVEL% to zero manually and then testing for an error after a START or TASKKILL command. But then I read that clearing %ERRORLEVEL% with SET ERRORLEVEL= is a better method. My issue comes in when I try to start the app with

START "" notepad.exe

Whenever I test the errorlevel after this command it is always greater than or equal to 1 unless I use SET ERRORLEVEL=0 before I run the start command. I have inserted the code for the four scripts below. Any insight and advice would be greatly appreciated.

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

解决方案

You should never SET the %errorlevel% variable. You are correct that there is a difference; The errorlevel that you get from an exiting process is an internal register that you can read with the %errorlevel% syntax. However, if you create a variable named ERRORLEVEL, it will mask the internal register and you lose access to the exit codes.

If you need to set the errorlevel register to a specific value, you can do it with the following command:

%comspec% /c exit %value%

This will spawn a process which immediately exits with the desired code.

这篇关于批处理编程,错误处理,并启动命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆