From cb7144264f65932963e9d2968e91b466eab62be2 Mon Sep 17 00:00:00 2001 From: Patrick Schleizer Date: Tue, 16 Dec 2014 18:49:17 +0000 Subject: [PATCH 1/1] implemented trap ERR for grml-debootstrap and chroot-script; changed chroot script from sh to bash script, because trap ERR is a bashism --- chroot-script | 10 +++++++++- grml-debootstrap | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/chroot-script b/chroot-script index a06aa7e..4d883f3 100755 --- a/chroot-script +++ b/chroot-script @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # Filename: /etc/debootstrap/chroot-script # Purpose: script executed in chroot when installing Debian via grml-debootstrap # Authors: grml-team (grml.org), (c) Michael Prokop @@ -9,6 +9,14 @@ # this script as /bin/chroot-script on your new installed system ################################################################################ +# error_handler {{{ +if [ "$REPORT_TRAP_ERR" = "yes" ] || [ "$FAIL_TRAP_ERR" = "yes" ]; then + set -E + set -o pipefail + trap "error_handler" ERR +fi +# }}} + . /etc/debootstrap/config || exit 1 . /etc/debootstrap/variables || exit 1 diff --git a/grml-debootstrap b/grml-debootstrap index 622a693..9b8c47d 100755 --- a/grml-debootstrap +++ b/grml-debootstrap @@ -6,6 +6,38 @@ # License: This file is licensed under the GPL v2+ ################################################################################ +# error_handler {{{ +[ -n "$REPORT_TRAP_ERR" ] || REPORT_TRAP_ERR='no' +[ -n "$FAIL_TRAP_ERR" ] || FAIL_TRAP_ERR='no' + +error_handler() { + last_exit_code="$?" + last_bash_command="$BASH_COMMAND" + if [ "$REPORT_TRAP_ERR" = "yes" ]; then + echo "Unexpected non-zero exit code $last_exit_code in $BASH_SOURCE at line $BASH_LINENO detected! +last bash command: $last_bash_command" + fi + if [ ! "$FAIL_TRAP_ERR" = "yes" ]; then + return + fi + ## Check if "bailout" function is available. + ## This is not the case in chroot-script. + if command -v bailout >/dev/null 2>&1; then + bailout 1 + else + echo 'FAIL_TRAP_ERR is set to "yes", exit 1.' + exit 1 + fi +} + +if [ "$REPORT_TRAP_ERR" = "yes" ] || [ "$FAIL_TRAP_ERR" = "yes" ]; then + set -E + set -o pipefail + trap "error_handler" ERR + export -f "error_handler" +fi +# }}} + # variables {{{ PN="$(basename "$0")" if [[ -d "$(dirname "$(which "$0")")"/.git ]]; then @@ -1403,7 +1435,7 @@ chrootscript() { mount --bind /dev "$MNTPOINT"/dev mount --bind /dev/pts "$MNTPOINT"/dev/pts if [ "$DEBUG" = "true" ] ; then - chroot "$MNTPOINT" /bin/sh -x /bin/chroot-script ; RC=$? + chroot "$MNTPOINT" /bin/bash -x /bin/chroot-script ; RC=$? else chroot "$MNTPOINT" /bin/chroot-script ; RC=$? fi -- 2.1.4