If-condition based on standard output











up vote
0
down vote

favorite












I have few commands linked through pipes, and at the end is a conditional awk: example below



command1 | command 2 | awk '$1 > 800'


Now sometimes it will output few lines, and sometimes no lines.
I want a condition that will prove true only if there's some output (1 or more lines)
Is there a way to make it work?



Like,



if command1 | command 2 | awk '$1 > 800' (some output); then
do command3
else; (blank output)
Do nothing









share|improve this question
























  • You could pipe the output into a while read construct. The while loop will iterate once for every line of output. Is that what you are looking for?
    – Bananguin
    Nov 14 at 11:45















up vote
0
down vote

favorite












I have few commands linked through pipes, and at the end is a conditional awk: example below



command1 | command 2 | awk '$1 > 800'


Now sometimes it will output few lines, and sometimes no lines.
I want a condition that will prove true only if there's some output (1 or more lines)
Is there a way to make it work?



Like,



if command1 | command 2 | awk '$1 > 800' (some output); then
do command3
else; (blank output)
Do nothing









share|improve this question
























  • You could pipe the output into a while read construct. The while loop will iterate once for every line of output. Is that what you are looking for?
    – Bananguin
    Nov 14 at 11:45













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have few commands linked through pipes, and at the end is a conditional awk: example below



command1 | command 2 | awk '$1 > 800'


Now sometimes it will output few lines, and sometimes no lines.
I want a condition that will prove true only if there's some output (1 or more lines)
Is there a way to make it work?



Like,



if command1 | command 2 | awk '$1 > 800' (some output); then
do command3
else; (blank output)
Do nothing









share|improve this question















I have few commands linked through pipes, and at the end is a conditional awk: example below



command1 | command 2 | awk '$1 > 800'


Now sometimes it will output few lines, and sometimes no lines.
I want a condition that will prove true only if there's some output (1 or more lines)
Is there a way to make it work?



Like,



if command1 | command 2 | awk '$1 > 800' (some output); then
do command3
else; (blank output)
Do nothing






scripting stdout






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 at 12:36









Kusalananda

116k15218351




116k15218351










asked Nov 14 at 11:39









Sollosa

1361315




1361315












  • You could pipe the output into a while read construct. The while loop will iterate once for every line of output. Is that what you are looking for?
    – Bananguin
    Nov 14 at 11:45


















  • You could pipe the output into a while read construct. The while loop will iterate once for every line of output. Is that what you are looking for?
    – Bananguin
    Nov 14 at 11:45
















You could pipe the output into a while read construct. The while loop will iterate once for every line of output. Is that what you are looking for?
– Bananguin
Nov 14 at 11:45




You could pipe the output into a while read construct. The while loop will iterate once for every line of output. Is that what you are looking for?
– Bananguin
Nov 14 at 11:45










3 Answers
3






active

oldest

votes

















up vote
3
down vote













I normally just use command substitution, then put it in a test, e.g.



if [ ! -z "$(command1 | command 2 | awk '$1 > 800')" ]; then command3; fi


Explanation




  • This runs the command as per your question: command1 | command 2 | awk '$1 > 800'

  • The output of this is passed to the test [ ! -z "$(…)" ], which will be true if it is not ! a string of zero length -z.


Hence, if there is output to the command pipe, the then commands will run.






share|improve this answer























  • Thanks @Sparhawk that is exactly what I was looking for.
    – Sollosa
    Nov 14 at 12:11


















up vote
1
down vote













Make the awk script exit with the correct return code for your if statement:



if command1 | command2 | awk '$1 > 800 { c++; print } END { exit (c == 0) }'
then
command3
fi


Or, if you don't actually need the output of the awk program:



if command1 | command2 | awk '$1 > 800 { c++; exit } END { exit (c == 0) }'
then
command3
fi





share|improve this answer























  • Could c wrap (or is awk arbitrary precision)?
    – ctrl-alt-delor
    Nov 14 at 13:03










  • Note that that exit would close the pipe and potentially cause command2 to be killed with SIGPIPE (which may or may not be desired)
    – Stéphane Chazelas
    Nov 14 at 13:11










  • @ctrl-alt-delor, awk typically uses long C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.
    – Stéphane Chazelas
    Nov 14 at 13:14












  • My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
    – ctrl-alt-delor
    Nov 14 at 13:18


















up vote
0
down vote













You can let awk print the desired exit code, like this:



echo 900 | awk '{ print !($1 > 800) }'


Then this can be wrapped in a subshell using ( and exit, for returning the value from awk:



echo 900 | (exit $(awk '{ print !($1 > 800) }'))


Which can then be used as part of a pipeline:



echo 900 | (exit $(awk '{ print !($1 > 800) }')) && echo yes || echo no





share|improve this answer





















    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%2f481684%2fif-condition-based-on-standard-output%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    I normally just use command substitution, then put it in a test, e.g.



    if [ ! -z "$(command1 | command 2 | awk '$1 > 800')" ]; then command3; fi


    Explanation




    • This runs the command as per your question: command1 | command 2 | awk '$1 > 800'

    • The output of this is passed to the test [ ! -z "$(…)" ], which will be true if it is not ! a string of zero length -z.


    Hence, if there is output to the command pipe, the then commands will run.






    share|improve this answer























    • Thanks @Sparhawk that is exactly what I was looking for.
      – Sollosa
      Nov 14 at 12:11















    up vote
    3
    down vote













    I normally just use command substitution, then put it in a test, e.g.



    if [ ! -z "$(command1 | command 2 | awk '$1 > 800')" ]; then command3; fi


    Explanation




    • This runs the command as per your question: command1 | command 2 | awk '$1 > 800'

    • The output of this is passed to the test [ ! -z "$(…)" ], which will be true if it is not ! a string of zero length -z.


    Hence, if there is output to the command pipe, the then commands will run.






    share|improve this answer























    • Thanks @Sparhawk that is exactly what I was looking for.
      – Sollosa
      Nov 14 at 12:11













    up vote
    3
    down vote










    up vote
    3
    down vote









    I normally just use command substitution, then put it in a test, e.g.



    if [ ! -z "$(command1 | command 2 | awk '$1 > 800')" ]; then command3; fi


    Explanation




    • This runs the command as per your question: command1 | command 2 | awk '$1 > 800'

    • The output of this is passed to the test [ ! -z "$(…)" ], which will be true if it is not ! a string of zero length -z.


    Hence, if there is output to the command pipe, the then commands will run.






    share|improve this answer














    I normally just use command substitution, then put it in a test, e.g.



    if [ ! -z "$(command1 | command 2 | awk '$1 > 800')" ]; then command3; fi


    Explanation




    • This runs the command as per your question: command1 | command 2 | awk '$1 > 800'

    • The output of this is passed to the test [ ! -z "$(…)" ], which will be true if it is not ! a string of zero length -z.


    Hence, if there is output to the command pipe, the then commands will run.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 at 12:18

























    answered Nov 14 at 11:51









    Sparhawk

    8,82763789




    8,82763789












    • Thanks @Sparhawk that is exactly what I was looking for.
      – Sollosa
      Nov 14 at 12:11


















    • Thanks @Sparhawk that is exactly what I was looking for.
      – Sollosa
      Nov 14 at 12:11
















    Thanks @Sparhawk that is exactly what I was looking for.
    – Sollosa
    Nov 14 at 12:11




    Thanks @Sparhawk that is exactly what I was looking for.
    – Sollosa
    Nov 14 at 12:11












    up vote
    1
    down vote













    Make the awk script exit with the correct return code for your if statement:



    if command1 | command2 | awk '$1 > 800 { c++; print } END { exit (c == 0) }'
    then
    command3
    fi


    Or, if you don't actually need the output of the awk program:



    if command1 | command2 | awk '$1 > 800 { c++; exit } END { exit (c == 0) }'
    then
    command3
    fi





    share|improve this answer























    • Could c wrap (or is awk arbitrary precision)?
      – ctrl-alt-delor
      Nov 14 at 13:03










    • Note that that exit would close the pipe and potentially cause command2 to be killed with SIGPIPE (which may or may not be desired)
      – Stéphane Chazelas
      Nov 14 at 13:11










    • @ctrl-alt-delor, awk typically uses long C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.
      – Stéphane Chazelas
      Nov 14 at 13:14












    • My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
      – ctrl-alt-delor
      Nov 14 at 13:18















    up vote
    1
    down vote













    Make the awk script exit with the correct return code for your if statement:



    if command1 | command2 | awk '$1 > 800 { c++; print } END { exit (c == 0) }'
    then
    command3
    fi


    Or, if you don't actually need the output of the awk program:



    if command1 | command2 | awk '$1 > 800 { c++; exit } END { exit (c == 0) }'
    then
    command3
    fi





    share|improve this answer























    • Could c wrap (or is awk arbitrary precision)?
      – ctrl-alt-delor
      Nov 14 at 13:03










    • Note that that exit would close the pipe and potentially cause command2 to be killed with SIGPIPE (which may or may not be desired)
      – Stéphane Chazelas
      Nov 14 at 13:11










    • @ctrl-alt-delor, awk typically uses long C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.
      – Stéphane Chazelas
      Nov 14 at 13:14












    • My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
      – ctrl-alt-delor
      Nov 14 at 13:18













    up vote
    1
    down vote










    up vote
    1
    down vote









    Make the awk script exit with the correct return code for your if statement:



    if command1 | command2 | awk '$1 > 800 { c++; print } END { exit (c == 0) }'
    then
    command3
    fi


    Or, if you don't actually need the output of the awk program:



    if command1 | command2 | awk '$1 > 800 { c++; exit } END { exit (c == 0) }'
    then
    command3
    fi





    share|improve this answer














    Make the awk script exit with the correct return code for your if statement:



    if command1 | command2 | awk '$1 > 800 { c++; print } END { exit (c == 0) }'
    then
    command3
    fi


    Or, if you don't actually need the output of the awk program:



    if command1 | command2 | awk '$1 > 800 { c++; exit } END { exit (c == 0) }'
    then
    command3
    fi






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 at 12:35

























    answered Nov 14 at 12:30









    Kusalananda

    116k15218351




    116k15218351












    • Could c wrap (or is awk arbitrary precision)?
      – ctrl-alt-delor
      Nov 14 at 13:03










    • Note that that exit would close the pipe and potentially cause command2 to be killed with SIGPIPE (which may or may not be desired)
      – Stéphane Chazelas
      Nov 14 at 13:11










    • @ctrl-alt-delor, awk typically uses long C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.
      – Stéphane Chazelas
      Nov 14 at 13:14












    • My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
      – ctrl-alt-delor
      Nov 14 at 13:18


















    • Could c wrap (or is awk arbitrary precision)?
      – ctrl-alt-delor
      Nov 14 at 13:03










    • Note that that exit would close the pipe and potentially cause command2 to be killed with SIGPIPE (which may or may not be desired)
      – Stéphane Chazelas
      Nov 14 at 13:11










    • @ctrl-alt-delor, awk typically uses long C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.
      – Stéphane Chazelas
      Nov 14 at 13:14












    • My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
      – ctrl-alt-delor
      Nov 14 at 13:18
















    Could c wrap (or is awk arbitrary precision)?
    – ctrl-alt-delor
    Nov 14 at 13:03




    Could c wrap (or is awk arbitrary precision)?
    – ctrl-alt-delor
    Nov 14 at 13:03












    Note that that exit would close the pipe and potentially cause command2 to be killed with SIGPIPE (which may or may not be desired)
    – Stéphane Chazelas
    Nov 14 at 13:11




    Note that that exit would close the pipe and potentially cause command2 to be killed with SIGPIPE (which may or may not be desired)
    – Stéphane Chazelas
    Nov 14 at 13:11












    @ctrl-alt-delor, awk typically uses long C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.
    – Stéphane Chazelas
    Nov 14 at 13:14






    @ctrl-alt-delor, awk typically uses long C compiler type for its integers which will generally be 64bit. It would take years for that number to wrap.
    – Stéphane Chazelas
    Nov 14 at 13:14














    My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
    – ctrl-alt-delor
    Nov 14 at 13:18




    My computer can do in a month, what would have taken millions of years in 1970. So if Mores law continues, millions of years is less than 50 years away.
    – ctrl-alt-delor
    Nov 14 at 13:18










    up vote
    0
    down vote













    You can let awk print the desired exit code, like this:



    echo 900 | awk '{ print !($1 > 800) }'


    Then this can be wrapped in a subshell using ( and exit, for returning the value from awk:



    echo 900 | (exit $(awk '{ print !($1 > 800) }'))


    Which can then be used as part of a pipeline:



    echo 900 | (exit $(awk '{ print !($1 > 800) }')) && echo yes || echo no





    share|improve this answer

























      up vote
      0
      down vote













      You can let awk print the desired exit code, like this:



      echo 900 | awk '{ print !($1 > 800) }'


      Then this can be wrapped in a subshell using ( and exit, for returning the value from awk:



      echo 900 | (exit $(awk '{ print !($1 > 800) }'))


      Which can then be used as part of a pipeline:



      echo 900 | (exit $(awk '{ print !($1 > 800) }')) && echo yes || echo no





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can let awk print the desired exit code, like this:



        echo 900 | awk '{ print !($1 > 800) }'


        Then this can be wrapped in a subshell using ( and exit, for returning the value from awk:



        echo 900 | (exit $(awk '{ print !($1 > 800) }'))


        Which can then be used as part of a pipeline:



        echo 900 | (exit $(awk '{ print !($1 > 800) }')) && echo yes || echo no





        share|improve this answer












        You can let awk print the desired exit code, like this:



        echo 900 | awk '{ print !($1 > 800) }'


        Then this can be wrapped in a subshell using ( and exit, for returning the value from awk:



        echo 900 | (exit $(awk '{ print !($1 > 800) }'))


        Which can then be used as part of a pipeline:



        echo 900 | (exit $(awk '{ print !($1 > 800) }')) && echo yes || echo no






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 at 12:31









        Alexander

        5,72812043




        5,72812043






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481684%2fif-condition-based-on-standard-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

            Morgemoulin

            Scott Moir

            Souastre