Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
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 Aug 10 '12 at 19:37
    
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 Aug 10 '12 at 20:27
    
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 Aug 11 '12 at 15:58
    
Ah I see that makes a lot more sense. Thank you :) –  Millianz Aug 16 '12 at 0:12

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.

share|improve this answer
    
There is only one batch script: foo.bat. The one you call the "outer" script is a Bash script not a batch script. –  Fran Aug 11 '12 at 21:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.