diff options
-rwxr-xr-x | with-tmpdir | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/with-tmpdir b/with-tmpdir new file mode 100755 index 0000000..412c8c6 --- /dev/null +++ b/with-tmpdir @@ -0,0 +1,27 @@ +#! /bin/sh +# usage: with-tmpdir [-t TEMPLATE] COMMAND [ARGS...] +# see mktemp(1) for TEMPLATE +set -efu + +template=with-tmpdir.XXXXXXXX + +case $1 in + -t) template=$2; shift 2;; +esac + +TMPDIR=$(mktemp -dt "$template") + +# make sure $TMPDIR exists and is a directory +(cd "$TMPDIR") + +cleanup() { + # ensure $PWD != $TMPDIR + cd / + rm -vR "$TMPDIR" +} + +trap 'cleanup; trap - EXIT INT QUIT' \ + EXIT INT QUIT + +export TMPDIR +"$@" |