2 # Author: Noah Friedman <friedman@prep.ai.mit.edu>
4 # Last modified: 1993-02-03
7 # Conversion to bash v2 syntax done by Chet Ramey
13 # Usage: mktmp [template] {createp}
15 # Generate a unique filename from TEMPLATE by appending a random number to
18 # If optional 2nd arg CREATEP is non-null, file will be created atomically
19 # before returning. This is to avoid the race condition that in between
20 # the time that the temporary name is returned and the caller uses it,
21 # someone else creates the file.
28 local tmpfile
="${template}${RANDOM}"
30 local noclobber_status
33 *C
*) noclobber_status
=set;;
36 if [ "${createp:+set}" = "set" ]; then
37 # Version which creates file atomically through noclobber test.
39 (> "${tmpfile}") 2> /dev
/null
40 while [ $?
-ne 0 ] ; do
41 # Detect whether file really exists or creation lost because of
42 # some other permissions problem. If the latter, we don't want
44 if [ ! -e "${tmpfile}" ]; then
45 # Trying to create file again creates stderr message.
46 echo -n "mktmp: " 1>&2
50 tmpfile
="${template}${RANDOM}"
51 (> "${tmpfile}") 2> /dev
/null
53 test "${noclobber_status}" != "set" && set +o noclobber
55 # Doesn't create file, so it introduces race condition for caller.
56 while [ -e "${tmpfile}" ]; do
57 tmpfile
="${template}${RANDOM}"
66 # mktmp.bash ends here