Skip to content

Instantly share code, notes, and snippets.

@krisk0
Created April 15, 2016 09:28
Show Gist options
  • Select an option

  • Save krisk0/af25a07d4804a850376c523c04d77d53 to your computer and use it in GitHub Desktop.

Select an option

Save krisk0/af25a07d4804a850376c523c04d77d53 to your computer and use it in GitHub Desktop.
mktemp implemented in .sh, understands 2 parameters tmpdir and suffix
#!/bin/sh
tmpdir=/tmp
unset suffix
while [ -n "$1" ]; do
case $1 in
--tmpdir=*)
tmpdir=${1#*=}
;;
--suffix=*)
suffix=${1#*=}
;;
*)
echo 'bad parameter' 1>&2
exit 1
esac
shift
done
while [ 1 ] ; do
f=$tmpdir/"$$"-${RANDOM}$suffix
[ -f $f ] && continue
>$f ||
(
echo "failed to create file %f" 1>&2
exit 1
)
echo $f
exit 0
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment