help-bash
[Top][All Lists]
Advanced

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

Re: try/catch in Bash (R. Diez)


From: Wiley Young
Subject: Re: try/catch in Bash (R. Diez)
Date: Mon, 8 Jun 2026 15:55:00 -0700

>
> On Sat, 6 Jun 2026 16:38:26 +0200, "R. Diez" wrote:


...Bash ignores errors by default, so that Bash scripts tend to very
> brittle.


Um, sort of....

More specifically, Bash by default avoids halting a script when an external
command returns a non-zero exit status. A shell error, however, such as a
syntax error, can halt a script.

Many scripts can be characterized as brittle, but writing robust scripts is
achievable although doing so can require more work.

Whether use of `set -e` is more beneficial or more detrimental has been a
matter of debate for some time. A common criticism of `set -e` is that it
contains a lot of "gotchas," while adherents insist that some
error-checking is better than none. Both of these positions hold true, in
my opinion.

Perhaps an alternative, What-You-See-Is-What-You-Get approach of just using
if-fi blocks around each central task could be something appropriate for
your purposes.

if _task_cmd_01_
then
  logger Task 1 succeeded
else
  logger Task 1 failed with exit status "$?"
  exit 1
fi

...or something like that.

Perhaps someone else can address the remaining concerns.

Wiley


I have become accustomed to doing the following first thing in my scripts:


> set -o errexit
> set -o nounset
> set -o pipefail
>
> I have a Bash script which runs every night in order to backup some files
> and do some maintenance. If one operation fails, it should not affect other
> operations. The operations should be able to write to stdout and stderr as
> usual.
>
> I came up with a try/catch routine like this:
>
> try_catch ()
> {
>    local CMD="$1"  # This can be any external command or a Bash function.
>                    # That is, anything that 'eval' can evaluate.
>    set +o errexit
>
>    ( # Run the command in a subshell.
>      set -o errexit  # Turn error detection back on in the subshell.
>      eval "$CMD"
>    )
>
>    CAUGHT_EXIT_CODE="$?"
>    set -o errexit
> }
>
> task1 ()
> {
> ...
> }
>
> task2 ()
> {
> ...
> }
>
> try_catch task1
>
> if (( CAUGHT_EXIT_CODE != 0 )); then
>    TASK_FAILURE_COUNT+=1
> fi
>
> try_catch task2
>
> if (( CAUGHT_EXIT_CODE != 0 )); then
>    TASK_FAILURE_COUNT+=1
> fi
>
> Is my try_catch approach a good idea? Or is there a better way?
>
> Actually, I have written a wrapper for try_catch called run_task which
> automatically handles TASK_FAILURE_COUNT+=1, measures the time each task
> takes, etc.
>
> The one problem I have now is that try_catch runs the given command in a
> subshell, so you cannot return anything in a variable.
>
> If a routine needs to return some value in a variable, should I write it
> to a temporary file and read that file after the subshell? Or is there a
> better way?
>
> Thanks in advance,
>    rdiez
>
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Help-bash mailing list
> Help-bash@gnu.org
> https://lists.gnu.org/mailman/listinfo/help-bash
>
>
> ------------------------------
>
> End of Help-bash Digest, Vol 176, Issue 3
> *****************************************
>


reply via email to

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