Last active
March 10, 2016 15:42
-
-
Save jiahaog/cf38c96dd0a1888a0bf5 to your computer and use it in GitHub Desktop.
Template for shell scripts with proper error catching
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/sh | |
# Template for shell scripts with proper error catching | |
# Modified from http://www.alittlemadness.com/2012/06/25/bash-tip-reliable-clean-up-with-trap/ | |
# immediately fail on error anywhere | |
set -e | |
# Defines a working area on the file system. | |
TEMP_DIR=/tmp/$$.scratch | |
function cleanUp() { | |
if [[ -d "${TEMP_DIR}" ]] | |
then | |
rm -r "${TEMP_DIR}" | |
fi | |
} | |
trap cleanUp EXIT | |
mkdir "${TEMP_DIR}" | |
# Do things below, placing temp files in $TEMP_DIR | |
# We succeeded, reset trap and clean up normally. | |
trap - EXIT | |
cleanUp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment