I am using pipelines to implement some functionality with bash on Centos 6. During these pipes I want to export my data in a log file, but this data comes from different pipes in my pipeline and I want to differentiate between them, by injecting a tag before each log line. To be more precise I want to do the following:
COMMAND1 | (create a line in the log file like: "command1: " + output of the command) | COMMAND2 ...
After doing a lot of research, I reached the following point:
COMMAND1 | tee >(ifne echo -n "command1: " >> out.log) | tee -a out.log | COMMAND2
This worked but with a problem. The second tee
writes first to the file and then writes the first tee
. So I get:
(output of command 1)
command1:
instead of
command1: (output of command 1)
I suspect that ifne
delays enough for the second tee
to be executed before the first one. If I remove the ifne
, it works fine. The problem is that I need the ifne
, because I often have null output, that I don't want to log.
How can I make the second tee wait for the first one to finish?