Created
April 15, 2016 09:28
-
-
Save krisk0/af25a07d4804a850376c523c04d77d53 to your computer and use it in GitHub Desktop.
mktemp implemented in .sh, understands 2 parameters tmpdir and suffix
This file contains hidden or 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
| #!/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