0

I want to execute one command after the commands above were all done.

In fact, the kkkk.exe is simply delay 5s and rrrr.exe is delay 9s. I have tried the following codes and it works, but I dont know why.

 ::run.bat

    echo start test
    (

    start kkkk.exe

    start rrrr.exe

    )|pause

    echo ppp

    pause

What I know is:

  • | : redirects the output of the first command to the input of the second command

  • It seems that | will make sure the commands before | are done, so that it have output for pause. But simply run this code, the pause echo will show;

    press any keys to continue...

    directly without waiting 9s(rrrr.exe). Why?

  • After 9s, I don't need to actually input any key, the echo ppp will automatically be run, why?

  • If I change | to &, I do need to input one key so that the echo ppp will be run, why?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 3
    Remove `start` (as this tells not to wait) or use `start /WAIT` (if you really insist in using `start`)... – aschipfl Aug 09 '19 at 08:53
  • 1
    @aschipfl: it works with a simple `start`, as long as none of the executables writes anything to the console. See also my comment above. – Stephan Aug 09 '19 at 10:15
  • 1
    You forgot to include a [link to the original code](https://stackoverflow.com/questions/33584587/how-to-wait-all-batch-files-to-finish-before-exiting/33586872#33586872) where _two_ extensive explanations of the method are given: one in the answer itself and a second, more technical description by user eryksun, appears in the comments below... – Aacini Aug 10 '19 at 19:08

1 Answers1

2

| : redirects the output of the first command to the input of the second command

correct.

It seems that | will make sure the commands before | are done, so that it have output for pause. But simply run this code, the pause echo will show;

press any keys to continue...

directly without waiting 9s(rrrr.exe). Why?

Both sides of the pipe are executed as own processes. Of course pause will show it's prompt (that's the purpose of pause) and then wait for input (from the other side of the pipe which will give an "End of Stream" signal when finished, which is interpreted by pause as "ENTER"). If you want, you can suppress the message with pause >nul

After 9s, I don't need to actually input any key, the echo ppp will automatically be run, why?

included in explanation above.

If I change | to &, I do need to input one key so that the echo ppp will be run, why?

That's because & just means "and then do", so pause won't receive a signal from the previous commands and so waits for the keyboard.

Note: your results may be different when using other executables. Depending on how they are programmed, start (or even start /wait) may return immediately. (Especially with GUI applications).

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • _"Also if one of the executables writes anything to the console, `pause` will receive that ("Any key") and continue..."_ If the executable is started via `start` command, then a separate `cmd.exe` console window is opened for it, so there is no way that the `pause` command could receive any input form such an executable... – Aacini Aug 10 '19 at 19:13
  • @Aacini: you're right. (wrong testing logic). Removed the wrong statement. – Stephan Aug 10 '19 at 20:05
  • However, when i add code `cout<<"k1"< – wantAsk2019 Aug 11 '19 at 12:48
  • `start` starts a new independent process, so any output of a `start`ed application has no influence on the pipe; while `call` executes the application within the same process. Therefore any output of a `call`ed application will be piped to the `pause`. – Stephan Aug 11 '19 at 18:13
  • So only with `start` it's possible to run several apps at the same time. `call` will always wait until the app ends before continuing with the next command. – Stephan Aug 11 '19 at 18:15