Discussion:
ash - exit code for a piped process
Alexander Kriegisch
2007-08-04 13:22:08 UTC
Permalink
I would like to determine the exit code of 'foo' in
foo | bar

I bash there is the PIPESTATUS array, but in ash (which I must use)
there is no such thing as PIPESTATUS or arrays. Is there any canonical
way or at least a workaround to achieve both piping the foo's output
into bar unconditionally *and* determining foo's exit code?

Thanks
--
Alexander Kriegisch
Loïc Grenié
2007-08-04 13:45:56 UTC
Permalink
Post by Alexander Kriegisch
I would like to determine the exit code of 'foo' in
foo | bar
I bash there is the PIPESTATUS array, but in ash (which I must use)
there is no such thing as PIPESTATUS or arrays. Is there any canonical
way or at least a workaround to achieve both piping the foo's output
into bar unconditionally *and* determining foo's exit code?
x=`((( foo 3>&- 4>&- ); echo $? >&3)| bar >&4 3>&- 4>&-) 3>&1` 4>&1

should do the trick. The exit code of foo is in x, the output of bar
goes to the output of the command.

Hope this helps.

Lo?c
Alexander Kriegisch
2007-08-04 14:16:44 UTC
Permalink
Post by Loïc Grenié
x=`((( foo 3>&- 4>&- ); echo $? >&3)| bar >&4 3>&- 4>&-) 3>&1` 4>&1
Thanks, Lo?c. I will consider your solution if I ever have a case like
this again, but my current workaround is rather simple to read and uses
a text buffer for the first command's output. I can do this because I
know that foo's output has a limited size which can be easily handled in
memory. So I choose this way for the sake of readability:

output=$(foo)
result=$?
echo "$output" | bar

I know the solution is somewhat limited, but a feasibly workaround in my
special case. Yours is more refined and really uses pipes.
--
Alexander Kriegisch
Tito
2007-08-04 14:24:18 UTC
Permalink
Post by Loïc Grenié
Post by Alexander Kriegisch
I would like to determine the exit code of 'foo' in
foo | bar
I bash there is the PIPESTATUS array, but in ash (which I must use)
there is no such thing as PIPESTATUS or arrays. Is there any canonical
way or at least a workaround to achieve both piping the foo's output
into bar unconditionally *and* determining foo's exit code?
x=`((( foo 3>&- 4>&- ); echo $? >&3)| bar >&4 3>&- 4>&-) 3>&1` 4>&1
should do the trick. The exit code of foo is in x, the output of bar
goes to the output of the command.
Hope this helps.
Lo?c
foo > out.file
ret=$?
cat out.file | bar
# rm out.file

Untested. Just an idea...........

Ciao,
Tito
Alexander Kriegisch
2007-08-04 14:16:44 UTC
Permalink
Post by Loïc Grenié
x=`((( foo 3>&- 4>&- ); echo $? >&3)| bar >&4 3>&- 4>&-) 3>&1` 4>&1
Thanks, Lo?c. I will consider your solution if I ever have a case like
this again, but my current workaround is rather simple to read and uses
a text buffer for the first command's output. I can do this because I
know that foo's output has a limited size which can be easily handled in
memory. So I choose this way for the sake of readability:

output=$(foo)
result=$?
echo "$output" | bar

I know the solution is somewhat limited, but a feasibly workaround in my
special case. Yours is more refined and really uses pipes.
--
Alexander Kriegisch
Tito
2007-08-04 14:24:18 UTC
Permalink
Post by Loïc Grenié
Post by Alexander Kriegisch
I would like to determine the exit code of 'foo' in
foo | bar
I bash there is the PIPESTATUS array, but in ash (which I must use)
there is no such thing as PIPESTATUS or arrays. Is there any canonical
way or at least a workaround to achieve both piping the foo's output
into bar unconditionally *and* determining foo's exit code?
x=`((( foo 3>&- 4>&- ); echo $? >&3)| bar >&4 3>&- 4>&-) 3>&1` 4>&1
should do the trick. The exit code of foo is in x, the output of bar
goes to the output of the command.
Hope this helps.
Lo?c
foo > out.file
ret=$?
cat out.file | bar
# rm out.file

Untested. Just an idea...........

Ciao,
Tito

Alexander Kriegisch
2007-08-04 13:22:08 UTC
Permalink
I would like to determine the exit code of 'foo' in
foo | bar

I bash there is the PIPESTATUS array, but in ash (which I must use)
there is no such thing as PIPESTATUS or arrays. Is there any canonical
way or at least a workaround to achieve both piping the foo's output
into bar unconditionally *and* determining foo's exit code?

Thanks
--
Alexander Kriegisch
Loïc Grenié
2007-08-04 13:45:56 UTC
Permalink
Post by Alexander Kriegisch
I would like to determine the exit code of 'foo' in
foo | bar
I bash there is the PIPESTATUS array, but in ash (which I must use)
there is no such thing as PIPESTATUS or arrays. Is there any canonical
way or at least a workaround to achieve both piping the foo's output
into bar unconditionally *and* determining foo's exit code?
x=`((( foo 3>&- 4>&- ); echo $? >&3)| bar >&4 3>&- 4>&-) 3>&1` 4>&1

should do the trick. The exit code of foo is in x, the output of bar
goes to the output of the command.

Hope this helps.

Lo?c
Continue reading on narkive:
Loading...