I have a question about the behavior of my bash command. I want to grep twice the same flux from stdout to 2 different files. To do so, I did:
./prog | tee >(grep -i 'grep1' > file1) | grep -i 'grep2' > file2
But my file2 is empty. I thought my grep -i 'grep2'
did not catch anything but if I just type:
./prog | tee >(grep -i 'grep1' > file1) | grep -i 'grep2'
I do have my attented result:
[grep2] mylog...
[grep2] mylog...
[grep2] mylog...
I manage to write in my two files with this command:
./prog | tee >(grep -i 'grep1' > file1) >(grep -i 'grep2' > file2)
but I have to add > /dev/null
at the end, to not have any output in stdout.
My question is, why does the redirection after the second grep is not catch by my file2
redirection and why do I have to add another named pipe to do so ?
After reading the answer of Kent and use his example using seq
I found the problem was my program.
My program behave as a deamon so the last pipe for the last redirection was not forked as the first one and so did nothing until the end of the program.
So I already had the right solution, using 2 named pipes for the 2 redirections:
./prog | tee >(grep -i 'grep1' > file1) >(grep -i 'grep2' > file2)