summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2016-02-02 14:58:45 +0100
committertv <tv@krebsco.de>2016-02-02 15:25:46 +0100
commita35a14f92e5727e5987a42a997931d2d991feb24 (patch)
tree944e8a32e8ef62a5ceac253d3352730289c40e8f
initial commit
-rwxr-xr-xwith-tmpdir27
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
+"$@"