Send stderr/stdout messages to function and trap exit signal

后端 未结 1 822
后悔当初
后悔当初 2021-01-21 17:05

Im working on error handling and logging in my bash script. Below I have included a simplified code snippet that exemplify the use case.

I want to achieve follow

相关标签:
1条回答
  • 2021-01-21 17:35

    You have to use

    set -o pipefail
    

    See this related StackOverflow Question.


    Minimal example:

    #!/bin/bash
    
    trap handler ERR
    handler() { echo trapped ; }
    
    echo 1
    false | :
    
    echo 2
    set -o pipefail
    false | :
    

    Output:

    $ bash test.sh
    1
    2
    trapped
    
    0 讨论(0)
提交回复
热议问题