File: on_exit.sh

package info (click to toggle)
python-os-xenapi 0.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,012 kB
  • sloc: python: 8,137; sh: 2,154; makefile: 45
file content (24 lines) | stat: -rwxr-xr-x 355 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash

set -e
set -o xtrace

if [ -z "${on_exit_hooks:-}" ]; then
    on_exit_hooks=()
fi

on_exit()
{
    for i in $(seq $((${#on_exit_hooks[*]} - 1)) -1 0); do
        eval "${on_exit_hooks[$i]}"
    done
}

add_on_exit()
{
    local n=${#on_exit_hooks[*]}
    on_exit_hooks[$n]="$*"
    if [[ $n -eq 0 ]]; then
        trap on_exit EXIT
    fi
}