考虑以下脚本:
#!/bin/bash
set -o pipefail
set -o history
trapper() {
func="$1" ; shift
for sig ; do
trap "$func $sig" "$sig"
done
}
err_handler () {
case $2 in
INT)
stop_received=1
;;
TSTP)
;;
ERR)
if [[ $2 != "INT" ]]; then # for some reason, ERR gets triggered on SIGINT
code=$?
if [ $code -ne 0 ]; then
echo "Failed on line $1"
echo "$BASH_COMMAND returned $?"
echo "Content of variables at the time of failure:"
echo "$(set -o …
Run Code Online (Sandbox Code Playgroud)