Copy stdout/stderr to a file

  • Follow


I want to run an executable and display its contents. Sometimes the
contents include the word "ERROR". I want to be able to run the
program, show all the contents, then see if the word Error is there. I
could do

program > somefile 2>&1
cat file
if [ -z `grep ERROR somefile` ] ; then
  echo "An error occurred"
   exit 1
fi

but that would not show the contents until after I'd run the program.
I'd like to show all the contents as the prgram runs, then afterwards
grep for the word ERROR.

I thought I could do this with "tee", but I don't seem to be getting
anywhere.

Any suggestions?

0
Reply Dr 9/6/2010 8:19:50 AM

On Mon, 6 Sep 2010 01:19:50 -0700 (PDT) "Dr. David Kirkby"
<david.kirkby@onetel.net> wrote:

> I want to run an executable and display its contents. Sometimes the
> contents include the word "ERROR". I want to be able to run the
> program, show all the contents, then see if the word Error is there. I
> could do
> 
> program > somefile 2>&1
> cat file
> if [ -z `grep ERROR somefile` ] ; then
>   echo "An error occurred"
>    exit 1
> fi
> 
> but that would not show the contents until after I'd run the program.
> I'd like to show all the contents as the prgram runs, then afterwards
> grep for the word ERROR.
> 
> I thought I could do this with "tee", but I don't seem to be getting
> anywhere.

It's not clear what you want to see with tee, but assuming you want to see
both output and errors (ie the same things that will go into "somefile"),
then try

program 2>&1 | tee somefile

0
Reply pk 9/6/2010 8:34:58 AM


On Mon, 06 Sep 2010 01:19:50 -0700, Dr. David Kirkby wrote:

> I want to run an executable and display its contents. Sometimes the
> contents include the word "ERROR". I want to be able to run the program,
> show all the contents, then see if the word Error is there. I could do
> 
> program > somefile 2>&1
> cat file
> if [ -z `grep ERROR somefile` ] ; then
>   echo "An error occurred"
>    exit 1
> fi
> 
> but that would not show the contents until after I'd run the program.
> I'd like to show all the contents as the prgram runs, then afterwards
> grep for the word ERROR.
> 
> I thought I could do this with "tee", but I don't seem to be getting
> anywhere.
> 
> Any suggestions?

The "obvious" thing is

program | tee somefile
if grep ERROR somefile >/dev/null
then
	# rm somefile
	echo An error occurred
	exit 1
fi
# rm somefile

If you have enough supporting structure then

exec 3>&1	# Make fd 3 a copy of stdout
if program | tee /dev/fd/3 | grep ERROR >/dev/null
then
	echo An error occurred
	exit 1
fi

assuming that you do not want the information saved in a file.
Depending on your grep, you may be able to use 'grep -q ...' instead of 
'grep ... >/dev/null', but I would tend to use the latter for portability.
0
Reply Icarus 9/6/2010 6:02:07 PM

2 Replies
396 Views

(page loaded in 0.057 seconds)

Similiar Articles:













7/24/2012 6:05:33 AM


Reply: