Is there a way to write a bash function which aborts the whole execution, no matter how it is called?

前端 未结 5 821
悲&欢浪女
悲&欢浪女 2020-11-29 18:19

I was using \"exit 1\" statement in my bash functions to terminate the whole script and it worked fine:

function func()
{
   echo \"Goodbye\"
   exit 1
}
ech         


        
5条回答
  •  囚心锁ツ
    2020-11-29 18:57

    A child process can't force the parent process to close implicitly. You need to use some kind of signaling mechanism. Options might include a special return value, or perhaps sending some signal with kill, something like

    function child() {
        local parent_pid="$1"
        local other="$2"
        ...
        if [[ $failed ]]; then
            kill -QUIT "$parent_pid"
        fi
    }
    

提交回复
热议问题