1850786 Members
3943 Online
104055 Solutions
New Discussion

"tee" command

 
Troyan Krastev
Regular Advisor

"tee" command

Hi Experts,

Take a look at this code:
-----------------------------------------------------------------------------
dev03:/ # ping yellow -n 2 | tee /tmp/1 | grep time
64 bytes from 172.16.19.17: icmp_seq=0. time=3. ms
64 bytes from 172.16.19.17: icmp_seq=1. time=1. ms
dev03:/ # cat /tmp/1
PING yellow.wc.ricoh.com: 64 byte packets
64 bytes from 172.16.19.17: icmp_seq=0. time=3. ms
64 bytes from 172.16.19.17: icmp_seq=1. time=1. ms

----yellow.wc.ricoh.com PING Statistics----
2 packets transmitted, 2 packets received, 0% packet loss
round-trip (ms) min/avg/max = 1/2/3
------------------------------------------------------------------------------
Every thing is the way it should be!
But now take a look at the same code where grep is with option "-q":
------------------------------------------------------------------------------
dev03: # ping yellow -n 2 | tee /tmp/1 | grep -q time
dev03: # cat /tmp/1
PING yellow.wc.ricoh.com: 64 byte packets
64 bytes from 172.16.19.17: icmp_seq=0. time=8. ms
------------------------------------------------------------------------------

Why the output of "cat /tmp/1" is different?

Thanks,
Troy.
5 REPLIES 5
Jeff Machols
Esteemed Contributor

Re: "tee" command

The grep is not being applied to the tee, thats why, you need to move the tee to last pipe.

ping node -n 2 | grep -q time | tee /tmp/1
Robin Wakefield
Honored Contributor

Re: "tee" command

Troy,

You command works fine on Solaris. It looks like an HP issue. For now, you'll have to:

ping yellow -n 2 > /tmp/1
grep -q time /tmp/1

Interestingly, this works too:

ping yellow -n 2 | tee /tmp/1 | tee /tmp/1 | grep -q time

Jeff, if you do it that way round, /tmp/1 will be empty.

Rgds, Robin.
Jeff Machols
Esteemed Contributor

Re: "tee" command

Yea, I thought thats what Troy was looking for, to match the output exactly with the file
Troyan Krastev
Regular Advisor

Re: "tee" command

Thanks Jeff,

I do not want to put "tee" at the end. I expected /tmp/1 from second code to be the same as from the first code. I friend of mine just resolved the issue. "grep -q" interrupt the process as soon as it find the first matching line.
Thank you Rumiana Boiajieva,
Troy.
Eugen Cocalea
Respected Contributor

Re: "tee" command

Hi,

Jeff, grep -q means 'quiet'. quiet means no output. You will tee nothing so the file will be empty.

grep -q exits with return 0 when the first match is found. This is suppose messes the 'ping' command.

on the computer named, let's say host1 I used

ping host2 -n 5 | tee file | grep -q time

and on host 2 I was doing a tcpdump ip src host1

guess what? only 2 icmp requests arrived at host2.

This means that grep returns 0 on the first occurence so the ping is stopped but first it sends the second request.

I will dig a bit deeper, it seems normal behaviour to me, but I don't understand exactly what 'normal' means.

E.
To Live Is To Learn