您的位置:首页 > 其它

bat脚本中执行一系列外部脚本或命令中途任意环节失败而结束整个脚本执行的处理方法

2013-12-07 17:56 591 查看
举一个常见例子:
在一个发布脚本中,进行realse操作可能需要如下几个步骤:

1. 调用build工具进行build
2. 使用SSH工具上传build出的jar包
3. 使用SSH工具向远程主机发布重启服务的命令

上述任何环节出错,则后续动作再执行已无意义,所以整个脚本应该停止执行,这种处理可以使用以下命令方式:
call 第三方脚本或程序1
if "%errorlevel%"=="1" goto :end

call 第三方脚本或程序2
if "%errorlevel%"=="1" goto :end

以下是一段示例代码:
@echo off

set profile=%1

if "%2"=="release" goto release

rem #######################################################################################

:release

echo.
echo ***************************************************************************************
echo BUILD ALL MODULES...
echo ***************************************************************************************

call mvn install -DskipTests=true -P%profile%
if "%errorlevel%"=="1" goto :releasefailed

echo.
echo ***************************************************************************************
echo DEPLOY CORE MODULE...
echo ***************************************************************************************

call its-core\target\classes\core-dev-tool.bat deploy
if "%errorlevel%"=="1" goto :releasefailed

echo.
echo ***************************************************************************************
echo DEPLOY CLIENT MODULE...
echo ***************************************************************************************

call its-client\target\classes\client-dev-tool.bat deploy
if "%errorlevel%"=="1" goto :releasefailed

echo.
echo ***************************************************************************************
echo DEPLOY SERVER MODULE...
echo ***************************************************************************************

call its-server\target\classes\server-dev-tool.bat deploy
if "%errorlevel%"=="1" goto :releasefailed

echo.
echo ***************************************************************************************
echo DEPLOY SDK MODULE...
echo ***************************************************************************************

call its-sdk\target\classes\sdk-dev-tool.bat deploy
if "%errorlevel%"=="1" goto :releasefailed

echo.
echo ***************************************************************************************
echo RESTART HBASE ...
echo ***************************************************************************************

call its-server\target\classes\server-dev-tool.bat restart-hbase
if "%errorlevel%"=="1" goto :releasefailed
goto releasesuccess

rem #######################################################################################

:releasesuccess
echo.
echo.
echo ***************************************************************************************
echo RELEASE SUCCESS!!
echo ***************************************************************************************
goto end

:releasefailed
echo.
echo.
echo ***************************************************************************************
echo RELEASE FAILED!!
echo ***************************************************************************************
goto end

:end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: