Trigger actions based on specific events.
Add to your bpm.ini
file the following dependency.
[dependencies]
on=*
Run bpm install
to add the library. Finally, use it in your scripts.
#!/usr/bin/env bash
. bpm
bpm::include on
Run a command when the currently executing script or subshell ends. This overwrites the EXIT
trap, but tries to do this in a polite way. If the trap is already assigned another command, that command is added to the list of commands to run at the end. Subsequent calls to on::exit
will keep adding to this array.
- $1 - The command to execute.
- $2-@ - Optional arguments to pass to the command.
Examples
# Download a file
curl -O http://example.com/installer.tar.gz
# When done, automatically clean it up.
on::exit rm -f /tmp/installer.tar.gz
# This triggers at the end of subshells as well.
touch file1
on::exit rm file1
(
touch file2
on::exit rm file2
)
ls file1 file2 # Only shows file1
Returns nothing.
Function that runs the on::exit
commands. This is set as the EXIT trap in Bash to ensure that it always executes at the end of a script or subshell.
Examples
# Set up the trap. This happens automatically when `on::exit` executes.
# The example shown here is only for how it is to be used within the
# library. Users of the library don't need to run this.
trap on::exitTrap EXIT
Returns nothing.
This project is placed under an MIT License.