bug-bash
[Top][All Lists]
Advanced

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

Re: set -e is not clearly documented


From: Dan Douglas
Subject: Re: set -e is not clearly documented
Date: Thu, 10 Oct 2013 22:16:31 -0500
User-agent: KMail/4.11.2 (Linux/3.11.3-pf; KDE/4.11.2; x86_64; ; )

On Monday, October 07, 2013 10:21:06 AM Chet Ramey wrote:
> The subshell command is clearly part of the || compound command.  The
> subshell `knows' that it is part of || and set -e has no effect.  This
> example has been discussed before, on this list and the austin-group
> Posix list, and the bash behavior is correct.
> 
> Chet

Have you considered that adding proper exception handling might not be a large
step? Bash just needs to expose fine-grained error information and provide a
trigger for user-defined code to interpret it.  

        function missingFile { echo 'Tried opening some non-existing file!' 
>&2; }
        function badCmd { echo 'Unknown command.' >&2; }
        function otherErr { printf 'Some unknown error was hit with code: %s\n' 
"$1" >&2; }

        typeset -A handlerMap=(
                [file_not_found]=missingFile
                [command_not_found]=badCmd
                # User maps any conditions Bash knows how to throw as needed.
        )

                # If not ERR, an enhanced variant triggered on any update to 
BASH_ERR
                # Can be used in a manner similar to the Ksh KEYBD trap.
        trap '"${handlerMap[$BASH_ERR]:-otherErr "$BASH_ERR"}"' ERR

                # Bash sets BASH_ERR to "file_not_found" and triggers ERR. 
User-defined
                # code does the rest.
        exec <file_that_does_not_exist

-- 
Dan Douglas



reply via email to

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