I'm trying to create a Bash script to calculate MD5 checksum of big files using different process. I learned that one should use & for that purpose.
At the same time, I wanted to capture the results of the check sum in different variables and write them in file in order to read them after. So, I wrote the following script "test_base.sh" and executed it using the command "sh ./test_base.sh" and the results were sent to the following file "test.txt" which was empty.
My OS is LUBUNTU 22.04 LTS.
Why the "test.txt" is empty?
Code of the "test_base.sh":
#!/bin/bash
md51=`md5sum -b ./source/test1.mp4|cut -b 1-32` &
md52=`md5sum -b ./source/test2.mp4|cut -b 1-32` &
wait
echo "md51=$md51">./test.txt
echo "md52=$md52">>./test.txt
Result of "test.txt":
md51=
md52=
Updated Answer
If you really, really want to avoid intermediate files, you can use GNU Parallel as suggested by @Socowi in the comments. So, if you run this:
you will get something like this, where
-kkeeps the output in order regardless of which one finishes first:Now, if you transpose the linefeed into a space, like this:
You will get:
You can then
readthis intobashvariables, using_for the interspersed parts you aren't interested in:Yes, this will fail if there are spaces or linefeeds in your filenames, but if required, you can make a successively more and more complicated command to deal with cases your question doesn't mention, but then you kind of miss the salient points of what I am suggesting.
Original Answer
bashdoesn’t really have the concept of awaiting the result of a promise. So you could go with something like: