Calling .bat from bash terminates loop

1

I'm having a problem that is extremely odd to me.

I have a loop that reads a file line by line that looks something like this:

while read line; do
  echo "Beginning of loop"

  ## Call to function that executes .bat file

  echo "End of loop"
done < $MANIFEST

echo Outside of Loop

I call the .bat file in a sub shell, so it shouldn't affect any of my variables. It looks something like this:

  $(CMD /c c.bat -c "$SCR_DIR/prepsdk.sh Build $apppath")

The weird thing is that the bat file terminates my loops somehow. Meaning I will see the following output:

Beginning of Loop Output from .bat End of Loop Outside of Loop

If the call to .bat is commented out I see

Beginning of Loop End of Loop Beginning of Loop End of Loop ..... Beginning of Loop End of Loop Outside of Loop

So the behavior is as expected with the .bat file not being called. What is it that the .bat file could be doing that causes the loop to terminate like this? It's not modifying the $MANIFEST file, I made sure of that.

Millianz

Posted 2012-08-10T18:46:59.113

Reputation: 81

Answers

2

The batch file could be consuming the rest of the data from file $MANIFEST, leaving the next execution of read line to fail when it sees EOF.

Fran

Posted 2012-08-10T18:46:59.113

Reputation: 4 774

In which case, the solution would be to read $MANIFEST on a different file descriptor than standard input: while read -u3 line; do ...; done 3< $MANIFEST – chepner – 2012-08-10T19:37:34.813

I don't understand how this bat file could possibly be modifying the manifest file, but using a different file descriptor solved the problem. – Millianz – 2012-08-10T20:27:29.660

The batch file wasn't modifying the file, it was reading from standard input -- everything from standard input, leaving nothing for the next invocation of read to get, so read exited with a non-zero status, terminating the loop. That was my theory. – Fran – 2012-08-11T15:58:08.160

Ah I see that makes a lot more sense. Thank you :) – Millianz – 2012-08-16T00:12:22.920

0

Try executing the batch file with call foo.bat.

For whatever reason if you execute a batch file inside another batch file without the call statement the outer batch file is terminated. I don't know why the default behavior was ever desirable.

Justin Dearing

Posted 2012-08-10T18:46:59.113

Reputation: 2 704

There is only one batch script: foo.bat. The one you call the "outer" script is a Bash script not a batch script. – Fran – 2012-08-11T21:00:58.160