grc
grc copied to clipboard
(grcat) exit code zero despite previous pipe command returning non-zero
(I know grcat isn't meant for public use but like i said in another ticket it's really convenient; if this is unsupported I'm just putting this ticket out there in case grcat becomes supported)
When I run:
mvn compile | grcat ~/conf.mvn
and the compilation fails, I get exit code zero. This makes it a bit difficult to run a script that checks whether the compilation succeeded before running the next command. For example, I often run this:
mvn compile | grcat ~/conf.mvn && sh deploy_my_app.sh
and when deploy_my_app.sh prints loads of stuff and I'm not looking at my screen I don't see the failed mvn command, so start tearing my hair out wondering my my app isn't working.
As a workaround, you can use $PIPESTATUS:
$ true | grcat ../.grc/conf.ping ; echo $PIPESTATUS
0
$ false | grcat ../.grc/conf.ping ; echo $PIPESTATUS
1
E.g.:
mvn compile | grcat ~/conf.mvn
[ $PIPESTATUS -eq 0 ] && sh deploy_my_app.sh
Thanks for the suggestion. Feel free to close the ticket.
This is actually a valid concern - however, note that cat would eat the exit status too, so it is a matter of how much compatibility with cat we want to maintain.
In addition to $PIPESTATUS, you might use set -o pipefail if your shell is bash.
Pipefail also works in zsh 5.0.8, thankfully for me.
foobar | cat && echo "Succeeded wrongly"
zsh: command not found: foobar
Succeeded wrongly
set -o pipefail; foobar | cat || echo "failed correctly"
zsh: command not found: foobar
failed correctly
I would suggest you maintain 100% compatibility with cat (well, minus the colourizing 👀).
In this manner, it's a drop-in replacement and scripts do not need to be reconfigured.