Help with make sure shell script execute instruction in flow

Hi,

I want to write a shell script to make sure all the instruction is executive in flow.
eg.
I want my shell script to run finish this two progress first:

./program input_file_1.txt > input_file_1.txt.out &
./program input_file_2.txt > input_file_2.txt.out &

After then, only run the following command by using the output result from the above progress:

cat *.txt.out > input_file.final

Below is the content of the shell script that I wrote:

#!/bin/sh
./program input_file_1.txt > input_file_1.txt.out &
./program input_file_2.txt > input_file_2.txt.out &
cat *.txt.out > input_file.final

Unfortunately, when I running the shell script above. It will automatic execute the "cat *.txt.out > input_file.final". Even though my "./program input_file_X.txt > input_file_X.txt.out &" is still running. End up, my "input_file.final" is empty since the first two progress is still running and without produce any output file yet :frowning:

Does anybody got any idea how to make sure the above shell script will start running "cat *.txt.out > input_file.final" only when the first two progress is run finish.

Thanks for any advice.

You have not said what shell you are using but all modern shells have a wait builtin which will do what you want.

Here is the relevant section from the bash manpage.

1 Like