You are not logged in.

#1 2015-09-23 23:10:21

SuperUser42
Member
Registered: 2015-09-23
Posts: 1

How to "hop" exit status through pipe

I'm trying to create a shell script like this:

mycommand1 | mycommand2 | mycommand3

'mycommand1' outputs a 0 or 1 exit status. 'mycommand2' always returns a 0 exit status.

I would want to send the return code of 'mycommand1' to 'mycommand3'.

Is there any bash trick that can help me without changing 'mycommand2' code?

Offline

#2 2015-09-23 23:35:17

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: How to "hop" exit status through pipe

Does the first command output a 0 or 1 or does it set an exit status?  Those are two different things which would be handled differently.

Did you write the second command, and can you modify it?  One very ugly solution would be the following:

stage1=$(command1)
ret=$?
stage2=$(command2 <<<$stage1)
exit $ret | command3 <<<$stage2

If any of these are compiled code rather than bash scripts, there are *much* nicer ways of dealing with this.

EDIT: I'm actually not sure if return codes are even passed through pipes.  So do you actually mean exit codes, or just output?  Output would be much easier.

EDIT2: It seems all my above code is nonsense as my suspicion was correct: return codes are not passed through pipes in the shell.  But again if any of these are compiled code, it is very easy to manage.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2015-09-24 07:51:33

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: How to "hop" exit status through pipe

SuperUser42 wrote:

I would want to send the return code of 'mycommand1' to 'mycommand3'.

What do you mean by this? Send it how?

Normally the exit status of the pipe is the exit status of the last command, in this case mycommand3. If you mean you want the exit status of the script to be the exit status of mycommand1 instead, you can use the array $PIPESTATUS in bash or $pipestatus in zsh:

#!/bin/bash
mycommand1 | mycommand2 | mycommand3
exit ${PIPESTATUS[0]}

Offline

#4 2015-09-24 10:58:32

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: How to "hop" exit status through pipe

Cool - I didn't know about PIPESTATUS, but that fits with that I suspected: exit codes are not passed through pipes.  So the fact that there are three programs in the pipeline are irrlevant, command2 doesn't even get the exit code of command1.  It does get the stdout of command1 on it's stdin, and this could be passed on to command3, but it's not clear if that's what you are asking for.

If that is what you want, in what way do you want it passed?  Does command2 have it's own output?  If so, how should the stdout of command1 and command2 be combined?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2015-09-24 11:39:20

Awebb
Member
Registered: 2010-05-06
Posts: 6,286

Re: How to "hop" exit status through pipe

The question is indeed incomplete. What kind of arguments does mycommand2 take? Does it only process the exit code? Does it take the piped STDOUT of mycommand1?

We should stop guessing at this point, let OP read http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html and return with new insights.

EDIT: And this
http://mywiki.wooledge.org/BashFAQ/002
http://tldp.org/LDP/abs/html/exit-status.html

Last edited by Awebb (2015-09-24 11:42:37)

Offline

Board footer

Powered by FluxBB