2

In a bash script, I'm using tools like conntrack and tcpdump to output results to a file, but wish to hide the standard message displayed on the first output line on the command line after executing the command/script.

message:

conntrack v1.0.0 (conntrack-tools): 6 flow entries have been shown.

After reading the bash man page, I've tried things like:

conntrack -L|grep "dport=6439" &> /tmp/file

No matter what redirect option I try, the conntrack message stating the number of flow entries is always displayed in the shell where I execute the script. Same thing goes for tcpdump where it lists the capturing device and number of packets captured, etc.

Of course, I could add clear after the conntrack or tcpdump commands to quickly hide the output, but that solution is ugly.

How can I hide these kinds of messages?

3
  • This can be useful for you too stackoverflow.com/a/549776/792066
    – Braiam
    Commented Jul 17, 2013 at 0:08
  • side note: /dev/null would probably be better
    – strugee
    Commented Jul 17, 2013 at 0:25
  • /dev/null wouldn't apply here because I want to redirect the standard output to file /tmp/file/
    – John B
    Commented Jul 18, 2013 at 0:08

1 Answer 1

6

Those messages are printed as errors (stderr) rather than regular output (stdout).

Only stdout gets piped, not stderr. So by having &> /tmp/file after grep, you are only merging the stdout and stderr output of grep itself, it will not merge the stderr of conntrack

For your example, you probably want something like:

conntrack -L 2>&1 | grep "dport=6439" > /tmp/file

stderr is file descriptor 2, and stdout is file descriptor 1. 2>&1 merges stderr into stdout.

By sticking 2>&1 on conntrack, the output gets merged before it is piped to grep. There is no need to merge again on grep.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .