If-condition based on standard output

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











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
    yesterday














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
    yesterday












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 yesterday









Kusalananda

115k15218350




115k15218350










asked yesterday









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
    yesterday
















  • 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
    yesterday















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
yesterday




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
yesterday










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
    yesterday

















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
    yesterday










  • 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
    yesterday










  • @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
    yesterday











  • 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
    yesterday

















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
      yesterday














    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
      yesterday












    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 yesterday

























    answered yesterday









    Sparhawk

    8,80863789




    8,80863789











    • Thanks @Sparhawk that is exactly what I was looking for.
      – Sollosa
      yesterday
















    • Thanks @Sparhawk that is exactly what I was looking for.
      – Sollosa
      yesterday















    Thanks @Sparhawk that is exactly what I was looking for.
    – Sollosa
    yesterday




    Thanks @Sparhawk that is exactly what I was looking for.
    – Sollosa
    yesterday












    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
      yesterday










    • 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
      yesterday










    • @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
      yesterday











    • 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
      yesterday














    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
      yesterday










    • 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
      yesterday










    • @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
      yesterday











    • 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
      yesterday












    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 yesterday

























    answered yesterday









    Kusalananda

    115k15218350




    115k15218350











    • Could c wrap (or is awk arbitrary precision)?
      – ctrl-alt-delor
      yesterday










    • 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
      yesterday










    • @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
      yesterday











    • 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
      yesterday
















    • Could c wrap (or is awk arbitrary precision)?
      – ctrl-alt-delor
      yesterday










    • 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
      yesterday










    • @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
      yesterday











    • 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
      yesterday















    Could c wrap (or is awk arbitrary precision)?
    – ctrl-alt-delor
    yesterday




    Could c wrap (or is awk arbitrary precision)?
    – ctrl-alt-delor
    yesterday












    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
    yesterday




    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
    yesterday












    @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
    yesterday





    @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
    yesterday













    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
    yesterday




    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
    yesterday










    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 yesterday









        Alexander

        5,71812043




        5,71812043



























             

            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

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

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay