Skip to content

add pipefail #763

Closed
Closed

Description

@aeosynth

http://www.gnu.org/software/bash/manual/html_node/Pipelines.html

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

this would make set even easier to work with:

if set foo (false | bar)

Activity

  1. ridiculousfish commented on Jun 9, 2013

    @ridiculousfish
    Member

    What might the syntax be for this?

  2. aeosynth commented on Jun 9, 2013

    @aeosynth
    ContributorAuthor

    set -U pipefail; see #805 for related discussion

  3. sugar700 commented on Jun 12, 2013

    @sugar700
    Contributor

    Variables seem to magical. Perhaps something like pipefail on or something. Besides, such option as universal would add configurability that fish shell wants to avoid.

  4. ridiculousfish commented on Jun 16, 2013

    @ridiculousfish
    Member

    Any reason to not make pipefail the default and only behavior?

  5. aeosynth commented on Jun 16, 2013

    @aeosynth
    ContributorAuthor

    backwards compatibility i suppose

  6. Magicloud commented on Jul 3, 2013

    @Magicloud

    Is not pipefail the way it works now? The $status IS the rightmost command's exit status. But in #902 , I'd like to konw the status of one certain command (in my case, it is the leftmost one). How to archive that?

    20130703Wed110846shida@ud43d7e638def514846cf~/s/FChart (master)                                                                                                                                                                       
    $ false
    20130703Wed110949shida@ud43d7e638def514846cf~/s/FChart (master)                                                                                                                                                                       
    $ echo $status
    1
    20130703Wed110952shida@ud43d7e638def514846cf~/s/FChart (master)                                                                                                                                                                       
    $ false | tee /tmp/t
    20130703Wed111000shida@ud43d7e638def514846cf~/s/FChart (master)                                                                                                                                                                       
    $ echo $status
    0
    
  7. ridiculousfish commented on Jul 3, 2013

    @ridiculousfish
    Member

    pipefail means that the $status resulting from the job is the boolean AND of every job in the pipeline. So in the case of false | tee /tmp/tmp.txt, the overall status will be zero only if false and tee succeeds.

    There's no proposed mechanism for getting the result of a particular command in the pipeline, but usually there's only one command in the pipeline that is likely to fail.

  8. Magicloud commented on Jul 3, 2013

    @Magicloud

    I see. I misunderstood the quote in first post.

  9. lilyball commented on Apr 25, 2015

    @lilyball
    Contributor

    I don't think we should make pipefail the default. For example, the command yes | head -10 exits with code 0, but with pipefail it exits with code 141 (due to SIGPIPE). More generally, tools like head that consume part of their input and then exit would report failure if the program providing the input tries to write any more data. This includes less (which doesn't read all its input if you quit early) and other pagers.

    Since this behavior is something that you'd really only want to use on specific pipelines, it seems like we should instead just have a syntax to request that a particular pipeline should evaluate to the and of the component jobs (instead of the default or). Some variation on the normal pipe operator (|). If we used && then I'd suggest |&& but we don't use that. I'm actually tempted to say we should just support foo | and bar for this behavior.

  10. ridiculousfish commented on Apr 25, 2015

    @ridiculousfish
    Member

    Very good point @ kballard. I hate to introduce more syntax like |&&, but the foo | and bar idea is kind of amazing!

  11. maxnordlund commented on May 8, 2015

    @maxnordlund
    Contributor

    I also like the foo | and bar syntax, feels most natural to me. It kinda mirrors foo; and bar but also pipes the output from foo to bar. Then foo | bar would be the same as foo | or bar.

  12. donm commented on Aug 25, 2015

    @donm

    To me, the foo | and bar syntax implies that bar will only be run if foo exits with status 0. Sort of similar to foo | ifne bar. This is different than pipefail behavior where all commands are run in a pipeline, possibly simultaneously, and only the exit status of the pipeline potentially changes.

    Also, would you have to type it after every pipe, and what happens if you don't?

    foo | and bar | and baz | and qux
    # vs
    foo | and bar | baz | and qux
    

    Rather than changing the behavior of pipes with a pipefail variable, the leftmost nonzero status could just always be recorded in a new variable similar to status. I can't think of a very good name, but something like status_pipefail or status_all.

  13. cben commented on Dec 21, 2015

    @cben
    Contributor
    1. Are there any common cases except SIGPIPE where left command failing is "normal"?
      I'm not sure I'd seriously propose a definition of "rightmost non-0 that didn't die from SIGPIPE" but it might just be very convenient.

    2. Is yes | head returning non-0 actually bad? It's obviously a problem in strict mode (Strict mode options #805) but otherwise, why would I care?

      OK, here is one case: find | grep -q '~$'; and echo "Backup files found.". grep -q exits as soon as it sees one match, causing SIGPIPE (unless find exits quickly, it's a race). But the purpose was to check only grep's status.

    OTOH consider if ls $DIR | not grep -q '~$'; echo "Definitely no backup files."; end: if you don't have permission to read $DIR, ls errors out and in this case it'd be wrong to claim "Definitely no backup files".
    So the exact purpose — test last command or all — depends on the use case.
    IMHO, testing all commands (i.e. pipefail) would be a saner default (if compatibility weren't a concern).

  14. krader1961 commented on Mar 18, 2017

    @krader1961
    Contributor

    Closing because we don't want to add a pipefail option ala bash and can't change the current behavior. Also, something like $pipestatus as discussed in issue #2039 is more flexible and a helper function could be provided to return zero if all commands in the pipeline exited successfully or the rightmost non-zero status.

  15. locked as resolved and limited conversation to collaborators on Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions