1753903 Members
9497 Online
108810 Solutions
New Discussion юеВ

Re: Return code

 
SOLVED
Go to solution
Danny Engelbarts
Frequent Advisor

Return code

Hi,

I've got a shell script which has the output of a command piped through to a function as follows;

command1 2>&1 | command2

this works perfect but i need to know the return code of the first command ... how do i get it?

Thanx, Danny.
4 REPLIES 4
Rainer_1
Honored Contributor

Re: Return code

I'm not sure if this is applicable for you but you can split the two commands ie:

command1 >/tmp/output1 2>&1
ret1=$?
command2 ret2=$?
rm /tmp/output1
Rainer_1
Honored Contributor
Solution

Re: Return code

Just another idea

command1 2>&1 ; echo $?>/tmp/ret1 | command2

ret2=$?
ret1=`cat /tmp/ret1`;rm /tmp/ret1
Danny Engelbarts
Frequent Advisor

Re: Return code

Rainer,

I'm adding a timestamp to the output lines of the first command so this solution won't work, but i might be able to come up with something like this, thnx!

Danny.
Danny Engelbarts
Frequent Advisor

Re: Return code

Rainer,

That's nearly correct ;-) there have to be parentheses around command1 and the echo like this;

(command1; echo $? > tempfile) | command2

RC1=$(< tempfile)
rm tempfile

this works!

Danny.