myhome/.config/nvim/plugged/bash-support.vim/bash-support/codesnippets/create-tempfile-with-trap
Marcin Woźniak fc7dc3f34a
Added bash-support
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
2020-11-16 12:54:24 +01:00

20 lines
505 B
Plaintext

#-----------------------------------------------------------------------
# cleanup temporary file in case of a keyboard interrupt (SIGINT)
# or a termination signal (SIGTERM)
#-----------------------------------------------------------------------
function cleanup_temp
{
[ -e $tmpfile ] && rm --force $tmpfile
exit 0
}
trap cleanup_temp SIGHUP SIGINT SIGPIPE SIGTERM
tmpfile=$(mktemp) || { echo "$0: creation of temporary file failed!"; exit 1; }
# use tmpfile ...
rm --force $tmpfile