bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Revisiting Error handling (errexit)


From: David
Subject: Re: Revisiting Error handling (errexit)
Date: Tue, 5 Jul 2022 09:43:55 +1000

On Mon, 4 Jul 2022 at 22:22, Yair Lenga <yair.lenga@gmail.com> wrote:

> I've tried to look into a minimal
> solution that will address the most common pitfall of errexit, where many
> sequences (e.g., series of commands in a function) will not properly
> "break" with 'errexit'. For example:
>
> function foo {
>     cat /missing/file   # e.g.: cat non-existing file.
>     action2   # Executed even if action 1 fail.
>     action3
> }
>
> set -oerrexit   # want to catch errors in 'foo'
> if ! foo ; then
>     # Error handling for foo failure
> fi

On Tue, 5 Jul 2022 at 04:34, Yair Lenga <yair.lenga@gmail.com> wrote:

> Thanks for taking the time to review my post. I do not want to start a
> thread about the problems with ERREXIT. Instead, I'm trying to advocate for
> a minimal solution.

Here's the minimal solution that I would use.

function foo {
    cat /missing/file || return 1
    action2   # not executed if action 1 fail
    action3 || return 2
}

foo
foo_exitstatus=$?
case "$foo_exitstatus" in
    0 ) echo "no error"
        ;;
    1 ) echo "handle first error"
        ;;
    2 ) echo "handle a different error"
        ;;
esac



reply via email to

[Prev in Thread] Current Thread [Next in Thread]