Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

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

Oct
3
0
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 "[email protected]:/var/git/%1.git"
MustSucceed git pull cc master --tags
 
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 "[email protected]:/var/git/%1.git"
git pull cc master --tags
quit 0

handleit:
echo Error in %1
quit 2
 
Back
Top