Last active
March 9, 2019 20:25
-
-
Save panos--/860071 to your computer and use it in GitHub Desktop.
Bourne Shell Temporary File Management
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
# 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