質問

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 ?

役に立ちましたか?

解決 2

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)

他のヒント

you are almost there. try this:

./prog | tee >(grep -i 'grep1' > file1) >(grep -i 'grep2' > file2)

if you want to skip the stdout output, do

./prog | tee >(grep -i 'grep1' > file1) >(grep -i 'grep2' > file2) > /dev/null

for example:

kent$  seq 30|tee >(grep 2 >a) >(grep 3 > b) > /dev/null                                                                                                                    

kent$  head a b
==> a <==
2
12
20
21
22
23
24
25
26
27

==> b <==
3
13
23
30
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top