HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
cancel
Showing results for 
Search instead for 
Did you mean: 

command | tee -a file

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

command | tee -a file

Hi

Here my command:

java -jar example.jar | tee -a /tmp/aa.log
echo $?

The problem here is that $? is always 0, of course, because the last command is tee (not my java command). How to catch the return value code for java command ?

Bests regards
Den
8 REPLIES
Ivan Ferreira
Honored Contributor

Re: command | tee -a file

Maybe you should change to:

java -jar example.jar >> /tmp/aa.log 2>&1
echo $?
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Leo The Cat
Regular Advisor

Re: command | tee -a file

Hi.

No I need the tee ....

Bests Regards
Den
Ivan Ferreira
Honored Contributor

Re: command | tee -a file

Then try using the PIPESTATUS variable:

java -jar example.jar | tee -a /tmp/aa.log
echo $PIPESTATUS

PIPESTATUS
An array variable (see Arrays below) containing a list of exit status values from the processes in the most recently-executed foreground pipeline (which may contain only a single command).
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Jared Middleton
Frequent Advisor

Re: command | tee -a file

Here are some examples I put together years ago...

UNIX RETURN CODE PRIMER
[tested on Red Hat Linux using bash-2.05]

In the examples below:
- it is assumed that you want to both display the output of the command (ls) and capture the exit code (a.k.a. return code value) in a variable (rc).
- a return code value other than "1" means the script did not work correctly.

# Basic (correct)
$ ls nonexistent 2>&1; rc=$?; echo $rc
ls: nonexistent: No such file or directory
1

# Capture return code after pipe (wrong)
$ ls nonexistent 2>&1 | tee -a /dev/null; rc=$?; echo $rc
ls: nonexistent: No such file or directory
0

# Capture return code after command (wrong)
$ ls nonexistent 2>&1; rc=$? | tee -a /dev/null; echo $rc
ls: nonexistent: No such file or directory
<-- blank

# Capture return code after command run in subshell (wrong)
$ (ls nonexistent 2>&1; rc=$?) | tee -a /dev/null; echo $rc
ls: nonexistent: No such file or directory
<-- blank

# Use I/O redirection with extra file descriptors (correct)
$ exec 3>&1; rc=`exec 4>&1; (ls nonexistent 2>&1; echo $? >&4) | tee -a /dev/null >&3`; exec 3>&-; echo $rc
ls: nonexistent: No such file or directory
1

# bash v2.0+ offers an easy-to-use PIPESTATUS array (correct)
$ ls nonexistent 2>&1 | tee -a /dev/null; echo ${PIPESTATUS[0]}
ls: nonexistent: No such file or directory
1

Regards,
Jared
Suraj K Sankari
Honored Contributor

Re: command | tee -a file

Hi,

If you dont want to show your output or first command then no need to give tee command.
tee command is showing the output of first command and send the same thing into the output file.

Suraj
James R. Ferguson
Acclaimed Contributor
Solution

Re: command | tee -a file

Hi Den:

Another way (using a temporary file):

# (ls nofile 2>&1;echo $? > /tmp/rc.$$)|tee -a /dev/null;echo $(
Also, Den, please don't forget to evalulate the responses to some of your previous questions:

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1335784

Regards!

...JRF...
Heironimus
Honored Contributor

Re: command | tee -a file

If you want to avoid temp files and bash-specific variables....

rc=$({ { your-command ; echo $? 1>&3 ;} | tee -a output.txt 1>&2 ;} 3>&1) 2>&1
echo "rc=$rc"
Leo The Cat
Regular Advisor

Re: command | tee -a file

Thanks Guys