Ignore very specific error message in bashHow to check whether a command such as curl completed without...

Best model for precedence constraints within scheduling problem

Does git delete empty folders?

Did Wernher von Braun really have a "Saturn V painted as the V2"?

Independence of Mean and Variance of Discrete Uniform Distributions

Indirect speech - breaking the rules of it

Why did St. Jerome use "virago" in Gen. 2:23?

Virtual destructor moves object out of rodata section

Lazy brainfuck programmer

Output with the same length always

Atmospheric methane to carbon

Angles between vectors of center of two incircles

My father gets angry everytime I pass Salam, that means I should stop saying Salam when he's around?

Radix2 Fast Fourier Transform implemented in C++

Check disk usage of files returned with spaces

To plot branch cut of logarithm

What is "super" in superphosphate?

Why is su world executable?

Is there a way to make the "o" keypress of other-window <C-x><C-o> repeatable?

Just one file echoed from an array of files

Why don't modern jet engines use forced exhaust mixing?

Rotate List by K places

Number of matrices with bounded products of rows and columns

Does the Temple of the Gods spell nullify critical hits?

Can the front glass be repaired of a broken lens?



Ignore very specific error message in bash


How to check whether a command such as curl completed without error?How to pipe the stdout of a command, depending on the result of the exit codeExpect script within bash & exit codesHow can I launch a command, wait 2 seconds and return the output without killing the command?How to catch and handle nonzero exit status within a Bash function?ffmpeg bash script segment templateHow to save psql error message output in bash variable?How to exit a shell script on error AND message the user?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







3















script.sh:



#!/bin/bash
my-bin-file-i-run

if [ $? -eq 0 ]
then
exit 0
else
if [[ >&2 == *"name_to_handle_at"* ]]; then
exit 0
fi
echo >&2
exit 1
fi


I'd like to run my command and if it throws an error which the message includes "name_to_handle_at" it will handle it like the script had no errors, all other errors should be shown as usual. Can't really get it to work.










share|improve this question




















  • 2





    Possible duplicate of How to check whether a command such as curl completed without error?

    – muru
    2 days ago











  • Some sort of exception handling for bash?

    – markgraf
    2 days ago











  • Something like if out=$(my-bin-file-i-run 2>&1); then ... elif [[ $out == .... ]] ...

    – muru
    2 days ago











  • Or even closer: unix.stackexchange.com/questions/477820/…

    – muru
    2 days ago











  • to capture only stderr in a variable, see: stackoverflow.com/questions/3130375/…

    – cas
    yesterday


















3















script.sh:



#!/bin/bash
my-bin-file-i-run

if [ $? -eq 0 ]
then
exit 0
else
if [[ >&2 == *"name_to_handle_at"* ]]; then
exit 0
fi
echo >&2
exit 1
fi


I'd like to run my command and if it throws an error which the message includes "name_to_handle_at" it will handle it like the script had no errors, all other errors should be shown as usual. Can't really get it to work.










share|improve this question




















  • 2





    Possible duplicate of How to check whether a command such as curl completed without error?

    – muru
    2 days ago











  • Some sort of exception handling for bash?

    – markgraf
    2 days ago











  • Something like if out=$(my-bin-file-i-run 2>&1); then ... elif [[ $out == .... ]] ...

    – muru
    2 days ago











  • Or even closer: unix.stackexchange.com/questions/477820/…

    – muru
    2 days ago











  • to capture only stderr in a variable, see: stackoverflow.com/questions/3130375/…

    – cas
    yesterday














3












3








3


1






script.sh:



#!/bin/bash
my-bin-file-i-run

if [ $? -eq 0 ]
then
exit 0
else
if [[ >&2 == *"name_to_handle_at"* ]]; then
exit 0
fi
echo >&2
exit 1
fi


I'd like to run my command and if it throws an error which the message includes "name_to_handle_at" it will handle it like the script had no errors, all other errors should be shown as usual. Can't really get it to work.










share|improve this question














script.sh:



#!/bin/bash
my-bin-file-i-run

if [ $? -eq 0 ]
then
exit 0
else
if [[ >&2 == *"name_to_handle_at"* ]]; then
exit 0
fi
echo >&2
exit 1
fi


I'd like to run my command and if it throws an error which the message includes "name_to_handle_at" it will handle it like the script had no errors, all other errors should be shown as usual. Can't really get it to work.







bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Karl MorrisonKarl Morrison

1401 gold badge1 silver badge7 bronze badges




1401 gold badge1 silver badge7 bronze badges











  • 2





    Possible duplicate of How to check whether a command such as curl completed without error?

    – muru
    2 days ago











  • Some sort of exception handling for bash?

    – markgraf
    2 days ago











  • Something like if out=$(my-bin-file-i-run 2>&1); then ... elif [[ $out == .... ]] ...

    – muru
    2 days ago











  • Or even closer: unix.stackexchange.com/questions/477820/…

    – muru
    2 days ago











  • to capture only stderr in a variable, see: stackoverflow.com/questions/3130375/…

    – cas
    yesterday














  • 2





    Possible duplicate of How to check whether a command such as curl completed without error?

    – muru
    2 days ago











  • Some sort of exception handling for bash?

    – markgraf
    2 days ago











  • Something like if out=$(my-bin-file-i-run 2>&1); then ... elif [[ $out == .... ]] ...

    – muru
    2 days ago











  • Or even closer: unix.stackexchange.com/questions/477820/…

    – muru
    2 days ago











  • to capture only stderr in a variable, see: stackoverflow.com/questions/3130375/…

    – cas
    yesterday








2




2





Possible duplicate of How to check whether a command such as curl completed without error?

– muru
2 days ago





Possible duplicate of How to check whether a command such as curl completed without error?

– muru
2 days ago













Some sort of exception handling for bash?

– markgraf
2 days ago





Some sort of exception handling for bash?

– markgraf
2 days ago













Something like if out=$(my-bin-file-i-run 2>&1); then ... elif [[ $out == .... ]] ...

– muru
2 days ago





Something like if out=$(my-bin-file-i-run 2>&1); then ... elif [[ $out == .... ]] ...

– muru
2 days ago













Or even closer: unix.stackexchange.com/questions/477820/…

– muru
2 days ago





Or even closer: unix.stackexchange.com/questions/477820/…

– muru
2 days ago













to capture only stderr in a variable, see: stackoverflow.com/questions/3130375/…

– cas
yesterday





to capture only stderr in a variable, see: stackoverflow.com/questions/3130375/…

– cas
yesterday










2 Answers
2






active

oldest

votes


















6














Your syntax is faulty as you can't just compare the standard error of some previously executed command with == like that.



One suggestion is to save the error stream to a file and then parse that:



#!/bin/bash

if ! my-bin-file-i-run 2>error.log; then
if ! grep -q -F 'name_to_handle_at' error.log; then
echo 'some error message' >&2
exit 1
fi
fi


This would run the command and redirect the standard error stream to a file called error.log. If the command terminates with an error, grep is used to look for the string name_to_handle_at in the log file. If that can't be found, an error message is printed and the script terminates with a non-zero exit status.



In any other case, the script terminates with a zero exit status.



If you want the error.log file to be removed when your script terminates, you may do so explicitly with rm error.log in the appropriate places, or with an EXIT trap:



#!/bin/bash

trap 'rm -f error.log' EXIT

if ! my-bin-file-i-run 2>error.log; then
if ! grep -q -F 'name_to_handle_at' error.log; then
echo 'some error message' >&2
exit 1
fi
fi





share|improve this answer




























  • Ah, interesting! Is there a way to do this with variables instead of error files by any chance?

    – Karl Morrison
    2 days ago






  • 2





    @KarlMorrison Capturing the output from a program is definitely possible with a command substitution, but since we don't know if this output may contain megabytes of data, or just a few lines of text, it may be safer to just send the data to a temporary file. The code also becomes a fair bit uglier (IMHO).

    – Kusalananda
    2 days ago













  • Your trap at the top changed my mind, thanks!

    – Karl Morrison
    13 hours ago











  • @KarlMorrison There's also nothing stopping you from using mktemp to create the name of the error log file (errlog=$(mktemp), then ... 2>"$errlog" etc.) This would likely create the file under /tmp which may be a memory mounted filesystem (no writes to disk).

    – Kusalananda
    13 hours ago



















0














You can pipe the stderr of your program to grep and use the (bash-specific) PIPESTATUS variable to distinguish between the 4 combinations of command succeeded / failed and command printed / did not print that error message:



{ your_command 2>&1 >&3 | grep your_error_message >/dev/null; } 3>&1
case ${PIPESTATUS[*]} in
0*) ;; # the command succeeded
*0) ;; # the command failed but printed the error message
esac


Example:



# usage wrapper pattern cmd args ...
wrapper(){
msg=$1; shift
{ "$@" 2>&1 >&3 | grep "$msg" >/dev/null; } 3>&1
case ${PIPESTATUS[*]} in
0*|*0) return 0;;
*) return "${PIPESTATUS[0]}";;
esac
}

# usage test_cmd status error_message
test_cmd(){ status=$1; shift; echo >&2 "$@"; return "$status"; }

$ wrapper foo test_cmd 13 foo; echo $?
0
$ wrapper foo test_cmd 13 bar; echo $?
13


Notes:



Do not replace the grep >/dev/null with grep -q; that will cause grep to exit at the first match and cause your command to be SIGPIPEd.



You can however put a ... | tee /dev/stderr | ... between your command and the grep; that will cause error messages to be both passed to grep and printed to stderr.



Many subpar programs (especially python scripts) write error messages to stdout instead of stderr; if that's the case you can simply use



your_command 2>&1 | ...


instead of all that { 2>&1 >&3 | ... } >&3 fd juggling.






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',
    autoActivateHeartbeat: false,
    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%2f535847%2fignore-very-specific-error-message-in-bash%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









    6














    Your syntax is faulty as you can't just compare the standard error of some previously executed command with == like that.



    One suggestion is to save the error stream to a file and then parse that:



    #!/bin/bash

    if ! my-bin-file-i-run 2>error.log; then
    if ! grep -q -F 'name_to_handle_at' error.log; then
    echo 'some error message' >&2
    exit 1
    fi
    fi


    This would run the command and redirect the standard error stream to a file called error.log. If the command terminates with an error, grep is used to look for the string name_to_handle_at in the log file. If that can't be found, an error message is printed and the script terminates with a non-zero exit status.



    In any other case, the script terminates with a zero exit status.



    If you want the error.log file to be removed when your script terminates, you may do so explicitly with rm error.log in the appropriate places, or with an EXIT trap:



    #!/bin/bash

    trap 'rm -f error.log' EXIT

    if ! my-bin-file-i-run 2>error.log; then
    if ! grep -q -F 'name_to_handle_at' error.log; then
    echo 'some error message' >&2
    exit 1
    fi
    fi





    share|improve this answer




























    • Ah, interesting! Is there a way to do this with variables instead of error files by any chance?

      – Karl Morrison
      2 days ago






    • 2





      @KarlMorrison Capturing the output from a program is definitely possible with a command substitution, but since we don't know if this output may contain megabytes of data, or just a few lines of text, it may be safer to just send the data to a temporary file. The code also becomes a fair bit uglier (IMHO).

      – Kusalananda
      2 days ago













    • Your trap at the top changed my mind, thanks!

      – Karl Morrison
      13 hours ago











    • @KarlMorrison There's also nothing stopping you from using mktemp to create the name of the error log file (errlog=$(mktemp), then ... 2>"$errlog" etc.) This would likely create the file under /tmp which may be a memory mounted filesystem (no writes to disk).

      – Kusalananda
      13 hours ago
















    6














    Your syntax is faulty as you can't just compare the standard error of some previously executed command with == like that.



    One suggestion is to save the error stream to a file and then parse that:



    #!/bin/bash

    if ! my-bin-file-i-run 2>error.log; then
    if ! grep -q -F 'name_to_handle_at' error.log; then
    echo 'some error message' >&2
    exit 1
    fi
    fi


    This would run the command and redirect the standard error stream to a file called error.log. If the command terminates with an error, grep is used to look for the string name_to_handle_at in the log file. If that can't be found, an error message is printed and the script terminates with a non-zero exit status.



    In any other case, the script terminates with a zero exit status.



    If you want the error.log file to be removed when your script terminates, you may do so explicitly with rm error.log in the appropriate places, or with an EXIT trap:



    #!/bin/bash

    trap 'rm -f error.log' EXIT

    if ! my-bin-file-i-run 2>error.log; then
    if ! grep -q -F 'name_to_handle_at' error.log; then
    echo 'some error message' >&2
    exit 1
    fi
    fi





    share|improve this answer




























    • Ah, interesting! Is there a way to do this with variables instead of error files by any chance?

      – Karl Morrison
      2 days ago






    • 2





      @KarlMorrison Capturing the output from a program is definitely possible with a command substitution, but since we don't know if this output may contain megabytes of data, or just a few lines of text, it may be safer to just send the data to a temporary file. The code also becomes a fair bit uglier (IMHO).

      – Kusalananda
      2 days ago













    • Your trap at the top changed my mind, thanks!

      – Karl Morrison
      13 hours ago











    • @KarlMorrison There's also nothing stopping you from using mktemp to create the name of the error log file (errlog=$(mktemp), then ... 2>"$errlog" etc.) This would likely create the file under /tmp which may be a memory mounted filesystem (no writes to disk).

      – Kusalananda
      13 hours ago














    6












    6








    6







    Your syntax is faulty as you can't just compare the standard error of some previously executed command with == like that.



    One suggestion is to save the error stream to a file and then parse that:



    #!/bin/bash

    if ! my-bin-file-i-run 2>error.log; then
    if ! grep -q -F 'name_to_handle_at' error.log; then
    echo 'some error message' >&2
    exit 1
    fi
    fi


    This would run the command and redirect the standard error stream to a file called error.log. If the command terminates with an error, grep is used to look for the string name_to_handle_at in the log file. If that can't be found, an error message is printed and the script terminates with a non-zero exit status.



    In any other case, the script terminates with a zero exit status.



    If you want the error.log file to be removed when your script terminates, you may do so explicitly with rm error.log in the appropriate places, or with an EXIT trap:



    #!/bin/bash

    trap 'rm -f error.log' EXIT

    if ! my-bin-file-i-run 2>error.log; then
    if ! grep -q -F 'name_to_handle_at' error.log; then
    echo 'some error message' >&2
    exit 1
    fi
    fi





    share|improve this answer















    Your syntax is faulty as you can't just compare the standard error of some previously executed command with == like that.



    One suggestion is to save the error stream to a file and then parse that:



    #!/bin/bash

    if ! my-bin-file-i-run 2>error.log; then
    if ! grep -q -F 'name_to_handle_at' error.log; then
    echo 'some error message' >&2
    exit 1
    fi
    fi


    This would run the command and redirect the standard error stream to a file called error.log. If the command terminates with an error, grep is used to look for the string name_to_handle_at in the log file. If that can't be found, an error message is printed and the script terminates with a non-zero exit status.



    In any other case, the script terminates with a zero exit status.



    If you want the error.log file to be removed when your script terminates, you may do so explicitly with rm error.log in the appropriate places, or with an EXIT trap:



    #!/bin/bash

    trap 'rm -f error.log' EXIT

    if ! my-bin-file-i-run 2>error.log; then
    if ! grep -q -F 'name_to_handle_at' error.log; then
    echo 'some error message' >&2
    exit 1
    fi
    fi






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 2 days ago









    terdon

    140k34 gold badges288 silver badges466 bronze badges




    140k34 gold badges288 silver badges466 bronze badges










    answered 2 days ago









    KusalanandaKusalananda

    160k18 gold badges316 silver badges502 bronze badges




    160k18 gold badges316 silver badges502 bronze badges
















    • Ah, interesting! Is there a way to do this with variables instead of error files by any chance?

      – Karl Morrison
      2 days ago






    • 2





      @KarlMorrison Capturing the output from a program is definitely possible with a command substitution, but since we don't know if this output may contain megabytes of data, or just a few lines of text, it may be safer to just send the data to a temporary file. The code also becomes a fair bit uglier (IMHO).

      – Kusalananda
      2 days ago













    • Your trap at the top changed my mind, thanks!

      – Karl Morrison
      13 hours ago











    • @KarlMorrison There's also nothing stopping you from using mktemp to create the name of the error log file (errlog=$(mktemp), then ... 2>"$errlog" etc.) This would likely create the file under /tmp which may be a memory mounted filesystem (no writes to disk).

      – Kusalananda
      13 hours ago



















    • Ah, interesting! Is there a way to do this with variables instead of error files by any chance?

      – Karl Morrison
      2 days ago






    • 2





      @KarlMorrison Capturing the output from a program is definitely possible with a command substitution, but since we don't know if this output may contain megabytes of data, or just a few lines of text, it may be safer to just send the data to a temporary file. The code also becomes a fair bit uglier (IMHO).

      – Kusalananda
      2 days ago













    • Your trap at the top changed my mind, thanks!

      – Karl Morrison
      13 hours ago











    • @KarlMorrison There's also nothing stopping you from using mktemp to create the name of the error log file (errlog=$(mktemp), then ... 2>"$errlog" etc.) This would likely create the file under /tmp which may be a memory mounted filesystem (no writes to disk).

      – Kusalananda
      13 hours ago

















    Ah, interesting! Is there a way to do this with variables instead of error files by any chance?

    – Karl Morrison
    2 days ago





    Ah, interesting! Is there a way to do this with variables instead of error files by any chance?

    – Karl Morrison
    2 days ago




    2




    2





    @KarlMorrison Capturing the output from a program is definitely possible with a command substitution, but since we don't know if this output may contain megabytes of data, or just a few lines of text, it may be safer to just send the data to a temporary file. The code also becomes a fair bit uglier (IMHO).

    – Kusalananda
    2 days ago







    @KarlMorrison Capturing the output from a program is definitely possible with a command substitution, but since we don't know if this output may contain megabytes of data, or just a few lines of text, it may be safer to just send the data to a temporary file. The code also becomes a fair bit uglier (IMHO).

    – Kusalananda
    2 days ago















    Your trap at the top changed my mind, thanks!

    – Karl Morrison
    13 hours ago





    Your trap at the top changed my mind, thanks!

    – Karl Morrison
    13 hours ago













    @KarlMorrison There's also nothing stopping you from using mktemp to create the name of the error log file (errlog=$(mktemp), then ... 2>"$errlog" etc.) This would likely create the file under /tmp which may be a memory mounted filesystem (no writes to disk).

    – Kusalananda
    13 hours ago





    @KarlMorrison There's also nothing stopping you from using mktemp to create the name of the error log file (errlog=$(mktemp), then ... 2>"$errlog" etc.) This would likely create the file under /tmp which may be a memory mounted filesystem (no writes to disk).

    – Kusalananda
    13 hours ago













    0














    You can pipe the stderr of your program to grep and use the (bash-specific) PIPESTATUS variable to distinguish between the 4 combinations of command succeeded / failed and command printed / did not print that error message:



    { your_command 2>&1 >&3 | grep your_error_message >/dev/null; } 3>&1
    case ${PIPESTATUS[*]} in
    0*) ;; # the command succeeded
    *0) ;; # the command failed but printed the error message
    esac


    Example:



    # usage wrapper pattern cmd args ...
    wrapper(){
    msg=$1; shift
    { "$@" 2>&1 >&3 | grep "$msg" >/dev/null; } 3>&1
    case ${PIPESTATUS[*]} in
    0*|*0) return 0;;
    *) return "${PIPESTATUS[0]}";;
    esac
    }

    # usage test_cmd status error_message
    test_cmd(){ status=$1; shift; echo >&2 "$@"; return "$status"; }

    $ wrapper foo test_cmd 13 foo; echo $?
    0
    $ wrapper foo test_cmd 13 bar; echo $?
    13


    Notes:



    Do not replace the grep >/dev/null with grep -q; that will cause grep to exit at the first match and cause your command to be SIGPIPEd.



    You can however put a ... | tee /dev/stderr | ... between your command and the grep; that will cause error messages to be both passed to grep and printed to stderr.



    Many subpar programs (especially python scripts) write error messages to stdout instead of stderr; if that's the case you can simply use



    your_command 2>&1 | ...


    instead of all that { 2>&1 >&3 | ... } >&3 fd juggling.






    share|improve this answer
































      0














      You can pipe the stderr of your program to grep and use the (bash-specific) PIPESTATUS variable to distinguish between the 4 combinations of command succeeded / failed and command printed / did not print that error message:



      { your_command 2>&1 >&3 | grep your_error_message >/dev/null; } 3>&1
      case ${PIPESTATUS[*]} in
      0*) ;; # the command succeeded
      *0) ;; # the command failed but printed the error message
      esac


      Example:



      # usage wrapper pattern cmd args ...
      wrapper(){
      msg=$1; shift
      { "$@" 2>&1 >&3 | grep "$msg" >/dev/null; } 3>&1
      case ${PIPESTATUS[*]} in
      0*|*0) return 0;;
      *) return "${PIPESTATUS[0]}";;
      esac
      }

      # usage test_cmd status error_message
      test_cmd(){ status=$1; shift; echo >&2 "$@"; return "$status"; }

      $ wrapper foo test_cmd 13 foo; echo $?
      0
      $ wrapper foo test_cmd 13 bar; echo $?
      13


      Notes:



      Do not replace the grep >/dev/null with grep -q; that will cause grep to exit at the first match and cause your command to be SIGPIPEd.



      You can however put a ... | tee /dev/stderr | ... between your command and the grep; that will cause error messages to be both passed to grep and printed to stderr.



      Many subpar programs (especially python scripts) write error messages to stdout instead of stderr; if that's the case you can simply use



      your_command 2>&1 | ...


      instead of all that { 2>&1 >&3 | ... } >&3 fd juggling.






      share|improve this answer






























        0












        0








        0







        You can pipe the stderr of your program to grep and use the (bash-specific) PIPESTATUS variable to distinguish between the 4 combinations of command succeeded / failed and command printed / did not print that error message:



        { your_command 2>&1 >&3 | grep your_error_message >/dev/null; } 3>&1
        case ${PIPESTATUS[*]} in
        0*) ;; # the command succeeded
        *0) ;; # the command failed but printed the error message
        esac


        Example:



        # usage wrapper pattern cmd args ...
        wrapper(){
        msg=$1; shift
        { "$@" 2>&1 >&3 | grep "$msg" >/dev/null; } 3>&1
        case ${PIPESTATUS[*]} in
        0*|*0) return 0;;
        *) return "${PIPESTATUS[0]}";;
        esac
        }

        # usage test_cmd status error_message
        test_cmd(){ status=$1; shift; echo >&2 "$@"; return "$status"; }

        $ wrapper foo test_cmd 13 foo; echo $?
        0
        $ wrapper foo test_cmd 13 bar; echo $?
        13


        Notes:



        Do not replace the grep >/dev/null with grep -q; that will cause grep to exit at the first match and cause your command to be SIGPIPEd.



        You can however put a ... | tee /dev/stderr | ... between your command and the grep; that will cause error messages to be both passed to grep and printed to stderr.



        Many subpar programs (especially python scripts) write error messages to stdout instead of stderr; if that's the case you can simply use



        your_command 2>&1 | ...


        instead of all that { 2>&1 >&3 | ... } >&3 fd juggling.






        share|improve this answer















        You can pipe the stderr of your program to grep and use the (bash-specific) PIPESTATUS variable to distinguish between the 4 combinations of command succeeded / failed and command printed / did not print that error message:



        { your_command 2>&1 >&3 | grep your_error_message >/dev/null; } 3>&1
        case ${PIPESTATUS[*]} in
        0*) ;; # the command succeeded
        *0) ;; # the command failed but printed the error message
        esac


        Example:



        # usage wrapper pattern cmd args ...
        wrapper(){
        msg=$1; shift
        { "$@" 2>&1 >&3 | grep "$msg" >/dev/null; } 3>&1
        case ${PIPESTATUS[*]} in
        0*|*0) return 0;;
        *) return "${PIPESTATUS[0]}";;
        esac
        }

        # usage test_cmd status error_message
        test_cmd(){ status=$1; shift; echo >&2 "$@"; return "$status"; }

        $ wrapper foo test_cmd 13 foo; echo $?
        0
        $ wrapper foo test_cmd 13 bar; echo $?
        13


        Notes:



        Do not replace the grep >/dev/null with grep -q; that will cause grep to exit at the first match and cause your command to be SIGPIPEd.



        You can however put a ... | tee /dev/stderr | ... between your command and the grep; that will cause error messages to be both passed to grep and printed to stderr.



        Many subpar programs (especially python scripts) write error messages to stdout instead of stderr; if that's the case you can simply use



        your_command 2>&1 | ...


        instead of all that { 2>&1 >&3 | ... } >&3 fd juggling.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        mosvymosvy

        15.9k2 gold badges19 silver badges51 bronze badges




        15.9k2 gold badges19 silver badges51 bronze badges

































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f535847%2fignore-very-specific-error-message-in-bash%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

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...