Arnold Roa
Arnold Roa

Reputation: 7748

windows batch .bat exiting before all commands executed

I've a .bat file which a sequence of instructions, however at some point the instructions are not runned anymore. This is a section of the latest run command:

gem update --system
gem install --no-ri --no-rdoc bundle
gem install --no-ri --no-rdoc bundler
bundle install
echo EVERYTHING OK
echo Configuring DB

The first command shows this and then the program is exited:

Latest version currently installed. Aborting.

So I tried to run all those commands in a single one:

gem update --system & gem .....  & bundle install & echo THIS IS PRINTED OK
echo THIS IS NOT PRINTED

I can't understand why the last echo is not getting printed, but all the commands wiht an & works.

Upvotes: 0

Views: 66

Answers (3)

Arnold Roa
Arnold Roa

Reputation: 7748

Each time a command is executed in batch file, it passes control of the execution flow to the child process.

To avoid this, we need to prefix CALL before each command:

call gem update --system
call gem install --no-ri --no-rdoc bundle
call gem install --no-ri --no-rdoc bundler
call bundle install

echo EVERYTHING OK
echo Configuring DB

Upvotes: 1

Harry Johnston
Harry Johnston

Reputation: 36348

When a batch file includes a command that is another batch file, the batch processor interprets that as an instruction to replace the current batch file with the specified one. This behaviour comes to us from the dark days of MS-DOS, but has been retained for backwards compatibility. (Which is probably also the only reason the batch processor still exists!)

If you want to run the other batch file and then continue, you must use the call command, e.g.,

call gem update

For best results, to ensure that no state leaks up from the batch file you're calling, run it in a child process:

cmd /c gem update

(It is rather unfortunate, IMO, that gem is a batch file rather than a proper executable.)

Upvotes: 0

soundslikeodd
soundslikeodd

Reputation: 1077

Looks like the command is exiting before the echo is executed. Try to use the CALL command in front of each command in the script.

Upvotes: 1

Related Questions