bat脚本中if子句的errorlevel,一个大坑

这几天在写svnhook,需要用到bat,这里只记录下吧:

rem  检查tag
svnlook changed %REPOS% -t %TXN% | awk "{if(NR==1){str=$0}else{str=str\" \"$0}}END{print str}" | findstr "tags\/" > nul
if %errorlevel% == 0 (
    svnlook log %REPOS% -t %TXN% | awk "{if(NR==1){str=$0}else{str=str\" \"$0}}END{print str}"  | grep -E "^\[.{3}.*\]\[.{3}.*\].{10}.*" > nul
    if %errorlevel% gtr 0 goto :err_action6
) else (
    svnlook log %REPOS% -t %TXN% | awk "{if(NR==1){str=$0}else{str=str\" \"$0}}END{print str}"  | grep -E "^\[.{3}.*\].{10}.*" > nul
    if %errorlevel% gtr 0 goto :err_action5
)

 

上面的if子句中errorlevel一直拿不到正确的返回值,最后无奈,改成:

rem  检查tag

svnlook changed %REPOS% -t %TXN% | awk "{if(NR==1){str=$0}else{str=str\" \"$0}}END{print str}" | findstr "tags\/" > nul

if %errorlevel% == 0 (

::tag格式

:echo "tag --------------------" 1>&2

goto :tag_check

 

) else (

::非tag格式

:echo "no  tag --------------------" 1>&2

goto :no_tag_check

)

 

:tag_check

svnlook log %REPOS% -t %TXN% | awk "{if(NR==1){str=$0}else{str=str\" \"$0}}END{print str}"  | grep -E "^\[.{3}.*\]\[.{3}.*\].{10}.*" > nul

if %errorlevel% gtr 0 goto :err_action6

 

:no_tag_check

svnlook log %REPOS% -t %TXN% | awk "{if(NR==1){str=$0}else{str=str\" \"$0}}END{print str}" | grep -E "^\[.{3}.*\].{10}.*" > nul

if %errorlevel% gtr 0 goto :err_action5

 

备注:if子句的errorlevel拿不到正确的值,只能将子句goto出来,写在最外层

你可能感兴趣的:(linux系统管理,svn配置)