Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

stderr stdout and return values

1 view
Skip to first unread message

JWR

unread,
Jun 4, 2003, 9:37:52 AM6/4/03
to
On behalf of a colleague, we are trying to capture the return value in
a script whilst having stderr and stdout returned to the screen and to
a file. Example below:

# cat test
a="fred
echo$a
# ./test 2>&1 | tee file
./test: syntax error at line 1 : `"' unmatched
# echo $?
0
# cat file
./test: syntax error at line 1 : `"' unmatched
#

Now obviously we have the error to screen and file but the return
value after echo $? is returning that our commands at the prompt were
successful but we need it to return that the script 'test' failed.

Thanks for your advance wisdom

Jean-Pierre Radley

unread,
Jun 4, 2003, 10:20:20 AM6/4/03
to
JWR typed (on Wed, Jun 04, 2003 at 06:37:52AM -0700):

The $? that is 0 (indicating success) is NOT the exit status of the
./test command which failed; it is the exit status of the tee command,
which succeeded.


--
JP

JWR

unread,
Jun 4, 2003, 1:11:23 PM6/4/03
to
Jean-Pierre Radley <j...@jpr.com> wrote in message news:<20030604142...@jpradley.jpr.com>...

JPR, sorry if I wasn't 100% clear, but that is my EXACT point. I want
to capture the return value of the ./test command regardless of
failure or not, but all we can get is the return value of tee. We
need to capture the stderr and sdtout and return value of ./test, and
once with have worked out all the syntax we can incorporate the
commands in a script which relies on the return value. Thanks

John DuBois

unread,
Jun 4, 2003, 2:25:32 PM6/4/03
to
JWR <no_spam...@lycos.co.uk> wrote:
> On behalf of a colleague, we are trying to capture the return value in
> a script whilst having stderr and stdout returned to the screen and to
> a file. Example below:
>
> # cat test
> a="fred
> echo$a
> # ./test 2>&1 | tee file
> ./test: syntax error at line 1 : `"' unmatched
> # echo $?
> 0
> # cat file
> ./test: syntax error at line 1 : `"' unmatched
> #
>
> Now obviously we have the error to screen and file but the return
> value after echo $? is returning that our commands at the prompt were
> successful but we need it to return that the script 'test' failed.
...

>I want to capture the return value of the ./test command regardless of
>failure or not, but all we can get is the return value of tee. We
>need to capture the stderr and sdtout and return value of ./test, and
>once with have worked out all the syntax we can incorporate the
>commands in a script which relies on the return value. Thanks

Some solutions:

$ result=`exec 3>&1; { ./test 2>&1; echo $? >&3; } | tee file`
$ echo $result
1

In ksh:

$ ./test 2>&1 |&
$ pid=$!
$ tee file <&p
$ wait $!
$ echo $?
1

In bash:
$ ./test 2>&1 | tee file
$ echo ${PIPESTATUS[0]}
1


John
--
John DuBois spc...@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/

Stuart J. Browne

unread,
Jun 4, 2003, 6:58:25 PM6/4/03
to

"John DuBois" <spc...@deeptht.armory.com> wrote in message
news:3ede399c$0$1103$8ee...@newsreader.tycho.net...

*blink* wow.. I do still have lots of learning to do with regards to
shells.. *grin* now where's that book!


0 new messages