Standard/canonical way to test whether foregoing pipeline produced output?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
1












The situation I have in mind has the following structure:



% some_command | [PRODUCED OUTPUT] || echo 'no output' >&2


Here [PRODUCED OUTPUT] stands for some as-yet-unspecified testing command, whose value should be true (i.e. "success") iff some_command produces any output at all.



Of course, some_command stands for an arbitrarily complex pipeline, and likewise, echo 'no output' >&2 stands for some arbitrary action to perform in case some_command produces no output.



Is there a standard test to do the job represented above by [PRODUCED OUTPUT]? grep -qm1 '.' comes close but also reports false on an input consisting of empty lines.



Unlike Check if pipe is empty and run a command on the data if it isn't I just want to discard the input if it's present, I don't need to preserve it.










share|improve this question



















  • 2




    What's hacky about | grep -q .? O.o
    – muru
    Apr 13 '16 at 14:47










  • @muru: hackiness, of course, is in the eye of the beholder, but in my book at least, any use of a tool for a purpose entirely different from what it was intended for, especially when the tool in question is a powerful one, and the purpose it's being used is a trivial one, qualifies as "hacky". And yes, a ton of stuff would qualify as "hacky" by that definition, and a lot of other stuff many people would call hacky does not meet this definition... Nothing wrong with a hack, if that's all one can do...
    – kjo
    Apr 13 '16 at 14:59










  • @muru (cont'd) ...or if no one is looking, but for code that will go in a script, I avoid hacks as much as possible, if I want keep the script easy-to-understand by others. YMMV, it's all a matter of opinion.
    – kjo
    Apr 13 '16 at 15:00










  • Isn't testing whether input has something the intended purpose of grep?
    – muru
    Apr 13 '16 at 15:01










  • I'm assuming you don't care about stderr output? (you'd redirect it after some_command, before the pipeline, if you wanted it to be involved?)
    – Jeff Schaller
    Apr 13 '16 at 15:04














up vote
4
down vote

favorite
1












The situation I have in mind has the following structure:



% some_command | [PRODUCED OUTPUT] || echo 'no output' >&2


Here [PRODUCED OUTPUT] stands for some as-yet-unspecified testing command, whose value should be true (i.e. "success") iff some_command produces any output at all.



Of course, some_command stands for an arbitrarily complex pipeline, and likewise, echo 'no output' >&2 stands for some arbitrary action to perform in case some_command produces no output.



Is there a standard test to do the job represented above by [PRODUCED OUTPUT]? grep -qm1 '.' comes close but also reports false on an input consisting of empty lines.



Unlike Check if pipe is empty and run a command on the data if it isn't I just want to discard the input if it's present, I don't need to preserve it.










share|improve this question



















  • 2




    What's hacky about | grep -q .? O.o
    – muru
    Apr 13 '16 at 14:47










  • @muru: hackiness, of course, is in the eye of the beholder, but in my book at least, any use of a tool for a purpose entirely different from what it was intended for, especially when the tool in question is a powerful one, and the purpose it's being used is a trivial one, qualifies as "hacky". And yes, a ton of stuff would qualify as "hacky" by that definition, and a lot of other stuff many people would call hacky does not meet this definition... Nothing wrong with a hack, if that's all one can do...
    – kjo
    Apr 13 '16 at 14:59










  • @muru (cont'd) ...or if no one is looking, but for code that will go in a script, I avoid hacks as much as possible, if I want keep the script easy-to-understand by others. YMMV, it's all a matter of opinion.
    – kjo
    Apr 13 '16 at 15:00










  • Isn't testing whether input has something the intended purpose of grep?
    – muru
    Apr 13 '16 at 15:01










  • I'm assuming you don't care about stderr output? (you'd redirect it after some_command, before the pipeline, if you wanted it to be involved?)
    – Jeff Schaller
    Apr 13 '16 at 15:04












up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





The situation I have in mind has the following structure:



% some_command | [PRODUCED OUTPUT] || echo 'no output' >&2


Here [PRODUCED OUTPUT] stands for some as-yet-unspecified testing command, whose value should be true (i.e. "success") iff some_command produces any output at all.



Of course, some_command stands for an arbitrarily complex pipeline, and likewise, echo 'no output' >&2 stands for some arbitrary action to perform in case some_command produces no output.



Is there a standard test to do the job represented above by [PRODUCED OUTPUT]? grep -qm1 '.' comes close but also reports false on an input consisting of empty lines.



Unlike Check if pipe is empty and run a command on the data if it isn't I just want to discard the input if it's present, I don't need to preserve it.










share|improve this question















The situation I have in mind has the following structure:



% some_command | [PRODUCED OUTPUT] || echo 'no output' >&2


Here [PRODUCED OUTPUT] stands for some as-yet-unspecified testing command, whose value should be true (i.e. "success") iff some_command produces any output at all.



Of course, some_command stands for an arbitrarily complex pipeline, and likewise, echo 'no output' >&2 stands for some arbitrary action to perform in case some_command produces no output.



Is there a standard test to do the job represented above by [PRODUCED OUTPUT]? grep -qm1 '.' comes close but also reports false on an input consisting of empty lines.



Unlike Check if pipe is empty and run a command on the data if it isn't I just want to discard the input if it's present, I don't need to preserve it.







shell-script zsh pipe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 13 '17 at 12:36









Community

1




1










asked Apr 13 '16 at 14:31









kjo

3,97993662




3,97993662







  • 2




    What's hacky about | grep -q .? O.o
    – muru
    Apr 13 '16 at 14:47










  • @muru: hackiness, of course, is in the eye of the beholder, but in my book at least, any use of a tool for a purpose entirely different from what it was intended for, especially when the tool in question is a powerful one, and the purpose it's being used is a trivial one, qualifies as "hacky". And yes, a ton of stuff would qualify as "hacky" by that definition, and a lot of other stuff many people would call hacky does not meet this definition... Nothing wrong with a hack, if that's all one can do...
    – kjo
    Apr 13 '16 at 14:59










  • @muru (cont'd) ...or if no one is looking, but for code that will go in a script, I avoid hacks as much as possible, if I want keep the script easy-to-understand by others. YMMV, it's all a matter of opinion.
    – kjo
    Apr 13 '16 at 15:00










  • Isn't testing whether input has something the intended purpose of grep?
    – muru
    Apr 13 '16 at 15:01










  • I'm assuming you don't care about stderr output? (you'd redirect it after some_command, before the pipeline, if you wanted it to be involved?)
    – Jeff Schaller
    Apr 13 '16 at 15:04












  • 2




    What's hacky about | grep -q .? O.o
    – muru
    Apr 13 '16 at 14:47










  • @muru: hackiness, of course, is in the eye of the beholder, but in my book at least, any use of a tool for a purpose entirely different from what it was intended for, especially when the tool in question is a powerful one, and the purpose it's being used is a trivial one, qualifies as "hacky". And yes, a ton of stuff would qualify as "hacky" by that definition, and a lot of other stuff many people would call hacky does not meet this definition... Nothing wrong with a hack, if that's all one can do...
    – kjo
    Apr 13 '16 at 14:59










  • @muru (cont'd) ...or if no one is looking, but for code that will go in a script, I avoid hacks as much as possible, if I want keep the script easy-to-understand by others. YMMV, it's all a matter of opinion.
    – kjo
    Apr 13 '16 at 15:00










  • Isn't testing whether input has something the intended purpose of grep?
    – muru
    Apr 13 '16 at 15:01










  • I'm assuming you don't care about stderr output? (you'd redirect it after some_command, before the pipeline, if you wanted it to be involved?)
    – Jeff Schaller
    Apr 13 '16 at 15:04







2




2




What's hacky about | grep -q .? O.o
– muru
Apr 13 '16 at 14:47




What's hacky about | grep -q .? O.o
– muru
Apr 13 '16 at 14:47












@muru: hackiness, of course, is in the eye of the beholder, but in my book at least, any use of a tool for a purpose entirely different from what it was intended for, especially when the tool in question is a powerful one, and the purpose it's being used is a trivial one, qualifies as "hacky". And yes, a ton of stuff would qualify as "hacky" by that definition, and a lot of other stuff many people would call hacky does not meet this definition... Nothing wrong with a hack, if that's all one can do...
– kjo
Apr 13 '16 at 14:59




@muru: hackiness, of course, is in the eye of the beholder, but in my book at least, any use of a tool for a purpose entirely different from what it was intended for, especially when the tool in question is a powerful one, and the purpose it's being used is a trivial one, qualifies as "hacky". And yes, a ton of stuff would qualify as "hacky" by that definition, and a lot of other stuff many people would call hacky does not meet this definition... Nothing wrong with a hack, if that's all one can do...
– kjo
Apr 13 '16 at 14:59












@muru (cont'd) ...or if no one is looking, but for code that will go in a script, I avoid hacks as much as possible, if I want keep the script easy-to-understand by others. YMMV, it's all a matter of opinion.
– kjo
Apr 13 '16 at 15:00




@muru (cont'd) ...or if no one is looking, but for code that will go in a script, I avoid hacks as much as possible, if I want keep the script easy-to-understand by others. YMMV, it's all a matter of opinion.
– kjo
Apr 13 '16 at 15:00












Isn't testing whether input has something the intended purpose of grep?
– muru
Apr 13 '16 at 15:01




Isn't testing whether input has something the intended purpose of grep?
– muru
Apr 13 '16 at 15:01












I'm assuming you don't care about stderr output? (you'd redirect it after some_command, before the pipeline, if you wanted it to be involved?)
– Jeff Schaller
Apr 13 '16 at 15:04




I'm assuming you don't care about stderr output? (you'd redirect it after some_command, before the pipeline, if you wanted it to be involved?)
– Jeff Schaller
Apr 13 '16 at 15:04










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










How about using read?



$ cat /dev/null | read pointless || echo no output
no output
$ echo something | read pointless || echo no output
$ printf "n" | read pointless || echo no output
$ printf " n" | read pointless || echo no output
$ false | read pointless || echo no output
no output


According to the Open Group definition:




EXIT STATUS



The following exit values shall be returned:



0



Successful completion.



>0



End-of-file was detected or an error occurred.







share|improve this answer






















  • This also report no output when read false.
    – cuonglm
    Apr 13 '16 at 14:53










  • How do you trigger that?
    – Jeff Schaller
    Apr 13 '16 at 14:56










  • @cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
    – Jeff Schaller
    Apr 14 '16 at 15:13










  • Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
    – cuonglm
    Apr 14 '16 at 15:19










  • Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe other read implementations have other error situations.
    – Jeff Schaller
    Apr 14 '16 at 15:43

















up vote
-1
down vote













For me worked great | read -n 1.



$ echo 'faw' | read -n 1 || echo 'no output'
$ echo | read -n 1 || echo 'no output'
$ printf '' | read -n 1 || echo 'no output'
no output
$ false | read -n 1 || echo 'no output'
no output
$





share|improve this answer








New contributor




keypress is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f276199%2fstandard-canonical-way-to-test-whether-foregoing-pipeline-produced-output%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    How about using read?



    $ cat /dev/null | read pointless || echo no output
    no output
    $ echo something | read pointless || echo no output
    $ printf "n" | read pointless || echo no output
    $ printf " n" | read pointless || echo no output
    $ false | read pointless || echo no output
    no output


    According to the Open Group definition:




    EXIT STATUS



    The following exit values shall be returned:



    0



    Successful completion.



    >0



    End-of-file was detected or an error occurred.







    share|improve this answer






















    • This also report no output when read false.
      – cuonglm
      Apr 13 '16 at 14:53










    • How do you trigger that?
      – Jeff Schaller
      Apr 13 '16 at 14:56










    • @cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
      – Jeff Schaller
      Apr 14 '16 at 15:13










    • Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
      – cuonglm
      Apr 14 '16 at 15:19










    • Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe other read implementations have other error situations.
      – Jeff Schaller
      Apr 14 '16 at 15:43














    up vote
    2
    down vote



    accepted










    How about using read?



    $ cat /dev/null | read pointless || echo no output
    no output
    $ echo something | read pointless || echo no output
    $ printf "n" | read pointless || echo no output
    $ printf " n" | read pointless || echo no output
    $ false | read pointless || echo no output
    no output


    According to the Open Group definition:




    EXIT STATUS



    The following exit values shall be returned:



    0



    Successful completion.



    >0



    End-of-file was detected or an error occurred.







    share|improve this answer






















    • This also report no output when read false.
      – cuonglm
      Apr 13 '16 at 14:53










    • How do you trigger that?
      – Jeff Schaller
      Apr 13 '16 at 14:56










    • @cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
      – Jeff Schaller
      Apr 14 '16 at 15:13










    • Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
      – cuonglm
      Apr 14 '16 at 15:19










    • Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe other read implementations have other error situations.
      – Jeff Schaller
      Apr 14 '16 at 15:43












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    How about using read?



    $ cat /dev/null | read pointless || echo no output
    no output
    $ echo something | read pointless || echo no output
    $ printf "n" | read pointless || echo no output
    $ printf " n" | read pointless || echo no output
    $ false | read pointless || echo no output
    no output


    According to the Open Group definition:




    EXIT STATUS



    The following exit values shall be returned:



    0



    Successful completion.



    >0



    End-of-file was detected or an error occurred.







    share|improve this answer














    How about using read?



    $ cat /dev/null | read pointless || echo no output
    no output
    $ echo something | read pointless || echo no output
    $ printf "n" | read pointless || echo no output
    $ printf " n" | read pointless || echo no output
    $ false | read pointless || echo no output
    no output


    According to the Open Group definition:




    EXIT STATUS



    The following exit values shall be returned:



    0



    Successful completion.



    >0



    End-of-file was detected or an error occurred.








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 14 '16 at 15:33

























    answered Apr 13 '16 at 14:47









    Jeff Schaller

    36k952119




    36k952119











    • This also report no output when read false.
      – cuonglm
      Apr 13 '16 at 14:53










    • How do you trigger that?
      – Jeff Schaller
      Apr 13 '16 at 14:56










    • @cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
      – Jeff Schaller
      Apr 14 '16 at 15:13










    • Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
      – cuonglm
      Apr 14 '16 at 15:19










    • Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe other read implementations have other error situations.
      – Jeff Schaller
      Apr 14 '16 at 15:43
















    • This also report no output when read false.
      – cuonglm
      Apr 13 '16 at 14:53










    • How do you trigger that?
      – Jeff Schaller
      Apr 13 '16 at 14:56










    • @cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
      – Jeff Schaller
      Apr 14 '16 at 15:13










    • Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
      – cuonglm
      Apr 14 '16 at 15:19










    • Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe other read implementations have other error situations.
      – Jeff Schaller
      Apr 14 '16 at 15:43















    This also report no output when read false.
    – cuonglm
    Apr 13 '16 at 14:53




    This also report no output when read false.
    – cuonglm
    Apr 13 '16 at 14:53












    How do you trigger that?
    – Jeff Schaller
    Apr 13 '16 at 14:56




    How do you trigger that?
    – Jeff Schaller
    Apr 13 '16 at 14:56












    @cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
    – Jeff Schaller
    Apr 14 '16 at 15:13




    @cuonglm, I don't want to mislead the OP if there's a case where this would be wrong -- what scenario were you thinking of?
    – Jeff Schaller
    Apr 14 '16 at 15:13












    Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
    – cuonglm
    Apr 14 '16 at 15:19




    Just because the read documentation said it returns false when EOF reach or error occur, so it's not 100% to make sure there's no output.
    – cuonglm
    Apr 14 '16 at 15:19












    Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe other read implementations have other error situations.
    – Jeff Schaller
    Apr 14 '16 at 15:43




    Fair point; I browsed through the bash source code for read and while I didn't get all the way to the bottom (stopped at zreadcintr) the only errors were for timeouts, array declaration failure, or invalid -u file descriptor. Maybe other read implementations have other error situations.
    – Jeff Schaller
    Apr 14 '16 at 15:43












    up vote
    -1
    down vote













    For me worked great | read -n 1.



    $ echo 'faw' | read -n 1 || echo 'no output'
    $ echo | read -n 1 || echo 'no output'
    $ printf '' | read -n 1 || echo 'no output'
    no output
    $ false | read -n 1 || echo 'no output'
    no output
    $





    share|improve this answer








    New contributor




    keypress is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      -1
      down vote













      For me worked great | read -n 1.



      $ echo 'faw' | read -n 1 || echo 'no output'
      $ echo | read -n 1 || echo 'no output'
      $ printf '' | read -n 1 || echo 'no output'
      no output
      $ false | read -n 1 || echo 'no output'
      no output
      $





      share|improve this answer








      New contributor




      keypress is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



















        up vote
        -1
        down vote










        up vote
        -1
        down vote









        For me worked great | read -n 1.



        $ echo 'faw' | read -n 1 || echo 'no output'
        $ echo | read -n 1 || echo 'no output'
        $ printf '' | read -n 1 || echo 'no output'
        no output
        $ false | read -n 1 || echo 'no output'
        no output
        $





        share|improve this answer








        New contributor




        keypress is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        For me worked great | read -n 1.



        $ echo 'faw' | read -n 1 || echo 'no output'
        $ echo | read -n 1 || echo 'no output'
        $ printf '' | read -n 1 || echo 'no output'
        no output
        $ false | read -n 1 || echo 'no output'
        no output
        $






        share|improve this answer








        New contributor




        keypress is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        keypress is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 2 days ago









        keypress

        991




        991




        New contributor




        keypress is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        keypress is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        keypress is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f276199%2fstandard-canonical-way-to-test-whether-foregoing-pipeline-produced-output%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay