From 1fc909dda013107d3b562f0d73dc3de34e7c9781 Mon Sep 17 00:00:00 2001 From: Salt Date: Thu, 4 Oct 2018 16:20:38 -0500 Subject: [PATCH] tmp: Use traps to increase robustness --- tmp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tmp b/tmp index d08bc88..ff585d1 100755 --- a/tmp +++ b/tmp @@ -6,19 +6,25 @@ # Distributed under terms of the MIT license. # -## Define variables +# Define variables name="$(basename "$0" .sh)" tmpdir="$(mktemp -d)" -## Define functions +# Verify them +[ -z ${tmpdir+x} ] && exit 2 +# Define functions function log() { [ -z ${1+x} ] && return 1 printf "${name}: $1\n" } -## Do the do +function cleanup() { + rm -rf "${tmpdir}" +} +trap "cleanup" EXIT +# Do the do log "This folder will be removed when this shell exits" ( cd "${tmpdir}" $SHELL ) -rm -rf "${tmpdir}" +cleanup