views:

59

answers:

2

Hi,

I know that log4j by default outputs to stderror.

I have been capturing the out put of my application with the following command:

application_to_run 2> log ; cat log | grep FATAL

Is there a way to capture the output without the auxiliary file?

+2  A: 

If you want both stdout and stderr, use:

( application_to_run 2>&1 ) | grep FATAL

If you want both stderr alone, you can use:

( application_to_run 2>&1 >/dev/null ) | grep FATAL

The first sends all output destined for file handle 2 (stderr) to file handle 1 (stdout), then pipes that through grep. The second does the same but also sends stdout to the bit bucket. This will work since redirection is a positional thing. First, stderr is redirected to the current stdout, then stdout is redirected to /dev/null.

paxdiablo
What's the point of running the command in a subshell?
tokland
This seams to do the trick. I was wondering that is the point of the brackets (subshell?) as well
Fork
Just habit on my part, probably not necessary in `bash` - I come from an age where the original Bourne shell was the latest and greatest. Although modern shells have far more advanced features (and less gotchas), I still find myself often reverting to the old ways. In fact, I can't even remember if there _was_ a positional problem in the early shells. It may just have been something from my mentors at the time. Way too close to retirement to start changing now :-) By all means test it without the subshell. If it works, it will save some wear-and-tear on your fingers and keyboard.
paxdiablo
+1  A: 

If you are asking how to redirect stderr to stdout so you can use it in a pipe, there are two ways I know of:

$ command 2>&1 | ...

$ command |& ..
tokland
tokland