Last active
January 10, 2022 14:25
-
-
Save adrienjoly/c096de6e8339d90b20269aa6d2c6bed2 to your computer and use it in GitHub Desktop.
Clean up with trap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e # will stop the script if any command fails with a non-zero exit code | |
function cleanup { | |
./teardown.sh || true # keep tearing down, even if file does not exist | |
echo "🧹 Cleaned up." | |
} | |
trap cleanup EXIT | |
./do-whatever.sh | |
# Even if do-whatever.sh fails, trap will run the cleanup function. | |
# In case of error, the script will return a non-zero exit code, as usual. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment