grc icon indicating copy to clipboard operation
grc copied to clipboard

(grcat) exit code zero despite previous pipe command returning non-zero

Open sarnobat opened this issue 8 years ago • 5 comments

(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.

sarnobat avatar Feb 13 '17 21:02 sarnobat

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

richarson avatar Feb 13 '17 22:02 richarson

Thanks for the suggestion. Feel free to close the ticket.

sarnobat avatar Feb 14 '17 04:02 sarnobat

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.

garabik avatar Feb 14 '17 09:02 garabik

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

sarnobat avatar Mar 03 '17 22:03 sarnobat

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.

jnovack avatar Jun 10 '20 09:06 jnovack