How to? Force batch to stop when error in ext command

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
Oct 13, 2009
3
0
#1
I am converting some Bash scripts that I wrote to BTM.
One of the problems was to make sure that, whenever an external command returns a non-zero return code, the script should stop and say something meaningful.
This is what I have produced, please tell me if there is a better way.

Code:
@echo off
setlocal
alias Usage `echo Usage: downloadrepo.btm package & quit 1`
alias MustSucceed `%1 %2 %3 %4 %5 %6 %7 %8 %9 %10 & IF "%ERRORLEVEL%" NE "0" ( echo Error in %1 & quit 2 )`

IF "%1" EQ "" .OR. "%2" NE "" Usage

mkdir %1
cd %1
MustSucceed git init
MustSucceed git remote add cc "myname@myrepository.org:/var/git/%1.git"
MustSucceed git pull cc master --tags
 

samintz

Scott Mintz
May 20, 2008
1,229
11
Solon, OH, USA
#2
You could use the ON ERRORLEVEL command.
Code:
@echo off
setlocal

IFF %1.==. .OR.  NOT %2.==. THEN
    echo Usage: downloadrepo.btm package
    quit 1
ENDIFF

mkdir %1
cd %1
ON ERRORLEVEL NE 0 GOTO handleit
git init
git remote add cc "myname@myrepository.org:/var/git/%1.git"
git pull cc master --tags
quit 0

handleit:
echo Error in %1
quit 2
 
Likes: kontamusse