Skip to content

Instantly share code, notes, and snippets.

@panos--
Last active March 9, 2019 20:25
Show Gist options
  • Save panos--/860071 to your computer and use it in GitHub Desktop.
Save panos--/860071 to your computer and use it in GitHub Desktop.
Bourne Shell Temporary File Management
# Implements a simple solution for easy creation and
# automatic disposal of temporary files.
TEMPFILE=
TEMPFILES=
get_tempfile() {
TEMPFILE="`mktemp`"
test "x$TEMPFILES" = x \
&& TEMPFILES="$TEMPFILE" \
|| TEMPFILES="$TEMPFILES:$TEMPFILE"
}
remove_tempfiles() {
echo "$TEMPFILES" | sed 's/:/\n/g' | while IFS= read -r f; do
rm -f "$f"
done
}
trap remove_tempfiles 0 1 2 3 15
# example:
get_tempfile
tmp="$TEMPFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment