functionshellsetexit

shell exit function for set -e


I have a script.command file with a shell script inside, so that I can double click it to execute in a new terminal window. When it finishes the script it automatically closes regardless of the exit code. Back in the days I used to put ifs after every troublesome command like this:

iCanCouseError
if [[ $? -ne 0 ]]; then
    read -p "Press enter to close"
    exit 1
fi

This was keeping the terminal open until I press a button, so that I could read the logs and determine what caused an exception.

But now I switched to a much more convenient method: set -e at the beginning of the file. It automatically exits the script with an error code if any command fails. But now I can't read logs before the terminal closes. I wonder if there is some way to set an exit function in addition to set -e that will always execute right before script exits, so that I could put read command there to stop terminal from closing in case of non-zero exit code.


Solution

  • Consider not using set -e as it's a mess (see https://mywiki.wooledge.org/BashFAQ/105) and also consider setting a trap to do something different for success vs other exit statuses instead of putting code below your "real" code, something like:

    sig_handler() {
        exit_status=$?
        echo "Doing signal-specific stuff"
        exit "$exit_status"
    }
    
    trap sig_handler INT HUP TERM QUIT
    
    iCanCouseError
    

    Google for better alternative trap functions/usage, there are plenty of suggestions out there for you to choose from dependent on what you want to do.