#!/bin/bash
# --- subshell_trap.sh -------------------------------------------------
# Example script for handling bash errors. Exit on error. Trap exit.
# This script is supposed to run in a subshell.
# See also: http://fvue.nl/wiki/Bash:_Error_handling
# Let shell functions inherit ERR trap. Same as `set -E'.
set -o errtrace
# Trigger error when expanding unset variables. Same as `set -u'.
set -o nounset
# Trap non-normal exit signals: 1/HUP, 2/INT, 3/QUIT, 15/TERM, ERR
# NOTE1: - 9/KILL cannot be trapped.
#+ - 0/EXIT isn't trapped because:
#+ - with ERR trap defined, trap would be called twice on error
#+ - with ERR trap defined, syntax errors exit with status 0, not 2
# NOTE2: Setting ERR trap does implicit `set -o errexit' or `set -e'.
trap onexit 1 2 3 15 ERR
#--- onexit() -----------------------------------------------------
# @param $1 integer (optional) Exit status. If not set, use `$?'
function onexit() {
local exit_status=${1:-$?}
echo Exiting $0 with $exit_status
exit $exit_status
}
# myscript
# Allways call `onexit' at end of script
onexit
20100827
Executed in subshell, trap on exit
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment