Access standard error on other side of ||?

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











up vote
2
down vote

favorite












I want to do something like:



command || log $error_from_last_command


Is there a way to use || and still access stderr like a pipe?



My intention here is to process the error message from command, using log, but only if command fails.
I'm reading through the marked duplicate but I don't see how to apply that to my situation.







share|improve this question






















  • Do you mean you want to both run the right-hand side command only if the left-hand side fails, and get the output of the lhs to the rhs? Or something else? I'm not sure what it is you mean, could you clarify this a bit?
    – ilkkachu
    Jan 22 at 15:29










  • @ilkkachu yes as you describe, log should only run if command fails. The input of log should be the failure message of command to be sent to server for reporting.
    – Philip Kirkbride
    Jan 22 at 15:31














up vote
2
down vote

favorite












I want to do something like:



command || log $error_from_last_command


Is there a way to use || and still access stderr like a pipe?



My intention here is to process the error message from command, using log, but only if command fails.
I'm reading through the marked duplicate but I don't see how to apply that to my situation.







share|improve this question






















  • Do you mean you want to both run the right-hand side command only if the left-hand side fails, and get the output of the lhs to the rhs? Or something else? I'm not sure what it is you mean, could you clarify this a bit?
    – ilkkachu
    Jan 22 at 15:29










  • @ilkkachu yes as you describe, log should only run if command fails. The input of log should be the failure message of command to be sent to server for reporting.
    – Philip Kirkbride
    Jan 22 at 15:31












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I want to do something like:



command || log $error_from_last_command


Is there a way to use || and still access stderr like a pipe?



My intention here is to process the error message from command, using log, but only if command fails.
I'm reading through the marked duplicate but I don't see how to apply that to my situation.







share|improve this question














I want to do something like:



command || log $error_from_last_command


Is there a way to use || and still access stderr like a pipe?



My intention here is to process the error message from command, using log, but only if command fails.
I'm reading through the marked duplicate but I don't see how to apply that to my situation.









share|improve this question













share|improve this question




share|improve this question








edited Jan 23 at 2:20

























asked Jan 22 at 15:09









Philip Kirkbride

2,2922369




2,2922369











  • Do you mean you want to both run the right-hand side command only if the left-hand side fails, and get the output of the lhs to the rhs? Or something else? I'm not sure what it is you mean, could you clarify this a bit?
    – ilkkachu
    Jan 22 at 15:29










  • @ilkkachu yes as you describe, log should only run if command fails. The input of log should be the failure message of command to be sent to server for reporting.
    – Philip Kirkbride
    Jan 22 at 15:31
















  • Do you mean you want to both run the right-hand side command only if the left-hand side fails, and get the output of the lhs to the rhs? Or something else? I'm not sure what it is you mean, could you clarify this a bit?
    – ilkkachu
    Jan 22 at 15:29










  • @ilkkachu yes as you describe, log should only run if command fails. The input of log should be the failure message of command to be sent to server for reporting.
    – Philip Kirkbride
    Jan 22 at 15:31















Do you mean you want to both run the right-hand side command only if the left-hand side fails, and get the output of the lhs to the rhs? Or something else? I'm not sure what it is you mean, could you clarify this a bit?
– ilkkachu
Jan 22 at 15:29




Do you mean you want to both run the right-hand side command only if the left-hand side fails, and get the output of the lhs to the rhs? Or something else? I'm not sure what it is you mean, could you clarify this a bit?
– ilkkachu
Jan 22 at 15:29












@ilkkachu yes as you describe, log should only run if command fails. The input of log should be the failure message of command to be sent to server for reporting.
– Philip Kirkbride
Jan 22 at 15:31




@ilkkachu yes as you describe, log should only run if command fails. The input of log should be the failure message of command to be sent to server for reporting.
– Philip Kirkbride
Jan 22 at 15:31










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










If you want to use the output of a program in another that only runs after the first command completed, it's probably easiest to store the output in a file.



errfile=$(mktemp)
if ! somecommand 2> "$errfile" ; then
log < "$errfile" # or log "$(cat "$errfile")" ?
fi
rm "$errfile"


Piping the output would require the commands to run at the same time, but we only get the exit code when the first command finishes.



log < "$errfile" above would of course direct the error message to stdin of log (like you'd get with a pipe). To get it as a command line argument, use log "$(cat "$errfile")" (one argument), or log $(cat "$errfile") (with word splitting, log sees multiple arguments), or log "$(< "$errfile")" (non-standard, works at least in Bash).






share|improve this answer






















  • Ah I see. It works with cat, if log in an STDIN-reader. :-)
    – Richard Neumann
    Jan 22 at 16:07

















up vote
0
down vote













If it would be sufficient for your purposes to know the exit code of the first element of the pipe (or any element of any pipe), you can avail yourself of the bash variable PIPESTATUS, which according to the bash man page is: "An array variable ... containing a list of exit status values from the processes in the most-recently-executed foreground pipeline ...".



I realize that technically you are asking for something a bit different, but you might consider whether using this variable might meet your need in a way you didn't originally anticipate.






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: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f418884%2faccess-standard-error-on-other-side-of%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    If you want to use the output of a program in another that only runs after the first command completed, it's probably easiest to store the output in a file.



    errfile=$(mktemp)
    if ! somecommand 2> "$errfile" ; then
    log < "$errfile" # or log "$(cat "$errfile")" ?
    fi
    rm "$errfile"


    Piping the output would require the commands to run at the same time, but we only get the exit code when the first command finishes.



    log < "$errfile" above would of course direct the error message to stdin of log (like you'd get with a pipe). To get it as a command line argument, use log "$(cat "$errfile")" (one argument), or log $(cat "$errfile") (with word splitting, log sees multiple arguments), or log "$(< "$errfile")" (non-standard, works at least in Bash).






    share|improve this answer






















    • Ah I see. It works with cat, if log in an STDIN-reader. :-)
      – Richard Neumann
      Jan 22 at 16:07














    up vote
    3
    down vote



    accepted










    If you want to use the output of a program in another that only runs after the first command completed, it's probably easiest to store the output in a file.



    errfile=$(mktemp)
    if ! somecommand 2> "$errfile" ; then
    log < "$errfile" # or log "$(cat "$errfile")" ?
    fi
    rm "$errfile"


    Piping the output would require the commands to run at the same time, but we only get the exit code when the first command finishes.



    log < "$errfile" above would of course direct the error message to stdin of log (like you'd get with a pipe). To get it as a command line argument, use log "$(cat "$errfile")" (one argument), or log $(cat "$errfile") (with word splitting, log sees multiple arguments), or log "$(< "$errfile")" (non-standard, works at least in Bash).






    share|improve this answer






















    • Ah I see. It works with cat, if log in an STDIN-reader. :-)
      – Richard Neumann
      Jan 22 at 16:07












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    If you want to use the output of a program in another that only runs after the first command completed, it's probably easiest to store the output in a file.



    errfile=$(mktemp)
    if ! somecommand 2> "$errfile" ; then
    log < "$errfile" # or log "$(cat "$errfile")" ?
    fi
    rm "$errfile"


    Piping the output would require the commands to run at the same time, but we only get the exit code when the first command finishes.



    log < "$errfile" above would of course direct the error message to stdin of log (like you'd get with a pipe). To get it as a command line argument, use log "$(cat "$errfile")" (one argument), or log $(cat "$errfile") (with word splitting, log sees multiple arguments), or log "$(< "$errfile")" (non-standard, works at least in Bash).






    share|improve this answer














    If you want to use the output of a program in another that only runs after the first command completed, it's probably easiest to store the output in a file.



    errfile=$(mktemp)
    if ! somecommand 2> "$errfile" ; then
    log < "$errfile" # or log "$(cat "$errfile")" ?
    fi
    rm "$errfile"


    Piping the output would require the commands to run at the same time, but we only get the exit code when the first command finishes.



    log < "$errfile" above would of course direct the error message to stdin of log (like you'd get with a pipe). To get it as a command line argument, use log "$(cat "$errfile")" (one argument), or log $(cat "$errfile") (with word splitting, log sees multiple arguments), or log "$(< "$errfile")" (non-standard, works at least in Bash).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 22 at 15:53

























    answered Jan 22 at 15:40









    ilkkachu

    49.8k674137




    49.8k674137











    • Ah I see. It works with cat, if log in an STDIN-reader. :-)
      – Richard Neumann
      Jan 22 at 16:07
















    • Ah I see. It works with cat, if log in an STDIN-reader. :-)
      – Richard Neumann
      Jan 22 at 16:07















    Ah I see. It works with cat, if log in an STDIN-reader. :-)
    – Richard Neumann
    Jan 22 at 16:07




    Ah I see. It works with cat, if log in an STDIN-reader. :-)
    – Richard Neumann
    Jan 22 at 16:07












    up vote
    0
    down vote













    If it would be sufficient for your purposes to know the exit code of the first element of the pipe (or any element of any pipe), you can avail yourself of the bash variable PIPESTATUS, which according to the bash man page is: "An array variable ... containing a list of exit status values from the processes in the most-recently-executed foreground pipeline ...".



    I realize that technically you are asking for something a bit different, but you might consider whether using this variable might meet your need in a way you didn't originally anticipate.






    share|improve this answer
























      up vote
      0
      down vote













      If it would be sufficient for your purposes to know the exit code of the first element of the pipe (or any element of any pipe), you can avail yourself of the bash variable PIPESTATUS, which according to the bash man page is: "An array variable ... containing a list of exit status values from the processes in the most-recently-executed foreground pipeline ...".



      I realize that technically you are asking for something a bit different, but you might consider whether using this variable might meet your need in a way you didn't originally anticipate.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        If it would be sufficient for your purposes to know the exit code of the first element of the pipe (or any element of any pipe), you can avail yourself of the bash variable PIPESTATUS, which according to the bash man page is: "An array variable ... containing a list of exit status values from the processes in the most-recently-executed foreground pipeline ...".



        I realize that technically you are asking for something a bit different, but you might consider whether using this variable might meet your need in a way you didn't originally anticipate.






        share|improve this answer












        If it would be sufficient for your purposes to know the exit code of the first element of the pipe (or any element of any pipe), you can avail yourself of the bash variable PIPESTATUS, which according to the bash man page is: "An array variable ... containing a list of exit status values from the processes in the most-recently-executed foreground pipeline ...".



        I realize that technically you are asking for something a bit different, but you might consider whether using this variable might meet your need in a way you didn't originally anticipate.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 22 at 16:18









        user1404316

        2,314520




        2,314520






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f418884%2faccess-standard-error-on-other-side-of%23new-answer', 'question_page');

            );

            Post as a guest













































































            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