2

I am running the following command:

[user@server ~]$ /usr/sbin/ntpdate -d IPREMOVEDFORSECURITY | egrep 'transmit timestamp' | tail -1 | awk '{print $4, $5, $6, $7, $8}'
host found : HOSTREMOVEDFORSECURITY
Tue, Feb 15 2011 12:38:38.321

So the issue is, I always get the "host found :" line no matter which type of redirect I use. I have tried the following:

0>/dev/null
1>/dev/null
2>/dev/null
2>&1
2>&/dev/null

And every other variation I can think of, or have been able to find online. All I need and want to be returned is the second line (timestamp) of that command output. I don't understand why I can't seem to affect the "host found:" line?!?!

I'm running these commands through perl, however I see the same behavior when running them directly in bash. I need to suppress that host: line because in perl, I'm running the command using backticks to assign the output to a variable, and output a clean response based on some other conditionals. No matter what, that host line shows and muddies up my script's output, though it doesn't affect what is actually assigned to the variable.

My assumption here is that this line is not using the normal STDIN, STDOUT, STDERR threads, but I can't seem to pin down any specific information on a 4th output method. Every site I've found yet only talks about those three.

If anyone has the answer to this, please please please let me know!

PS: this isn't the only command that shows this behavior, chkconfig and some others also do this, and I'd really like to figure out how to suppress this unnecessary output.

Garrett
  • 23
  • 2

1 Answers1

6

ntpdate is printing the 'host found' to stderr and everything else to stdout. This will redirect everything to stdout:

/usr/sbin/ntpdate -d localhost 2>&1 | egrep 'transmit timestamp' 2>/dev/null | \
tail -1 | awk '{print $4, $5, $6, $7, $8}'

and then the egrep removes the 'host found' line since it doesn't match.

I think your confusion probably comes from putting the redirect at the end of the pipeline instead of inside the pipeline. you have to find the command that is printing the extra data to stderr (in this case ntpdate) and redirect or suppress stderr at that point, before the rest of the pipeline is executed.

Phil Hollenback
  • 14,647
  • 4
  • 34
  • 51
  • 1
    Elegant. More specifically, once stderr goes into the first pipe it gets starts getting processed by stdin/stdout for the rest of the command sequence. – Scott Pack Feb 15 '11 at 20:21
  • So the redirect cascades through the piped commands? – Garrett Feb 15 '11 at 21:17
  • Garret, the redirect just smooshes stderr from original command into stdout, so there's just one stream (stdout) coming out of the ntpdate command and being fed into next command. – Phil Hollenback Mar 03 '11 at 05:32