You are not logged in.
Pages: 1
You can run something like this in bash:
command1 && foo || bar
1. foo will run only when command1 succeeds (exit code 0)
2. bar will run only when command1 or foo succeeds.
Random example:
test -f ~/.config && echo "config already exists!" || touch ~/.config
My question is: Is there a straightforward way to pipe command1 into e.g. tail and still using the exit code of command1? This way doesn't work since the exit code of tail is used which is always 0.
command1 | tail && foo || bar
My specific situation looks like this: I need a different exit code of the whole script (or possibly no exit at all), depending on the exit code of command1.
command1 | sed 's/a/b/;' && exit $X || exit $Y
"hut_" or "h00th00t" in irc.freenode.net #archlinux
Ranger Mailing List: https://lists.nongnu.org/mailman/listinfo/ranger-users
Offline
The exitcodes of all commands in a pipe are in the PIPESTATUS-array.
Offline
Well thank you very much. I'll do something like this then:
command1 | tail
test ${PIPESTATUS[0]} == 0 && foo || bar
"hut_" or "h00th00t" in irc.freenode.net #archlinux
Ranger Mailing List: https://lists.nongnu.org/mailman/listinfo/ranger-users
Offline
Wow, didn't know about PIPESTATUS. Neat-o.
Alternatively, you can enable the shell option pipefail. From bash(1):
The return status of a pipeline is the exit status of the last command, unless the pipefail option is enabled. If pipefail is enabled, the
pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit success‐
fully. If the reserved word ! precedes a pipeline, the exit status of that pipeline is the logical negation of the exit status as
described above. The shell waits for all commands in the pipeline to terminate before returning a value.
Offline
also check out mispipe in the moreutils package
Offline
Pages: 1