mktemp revision
- To: mathgroup at yoda.physics.unc.edu
- Subject: mktemp revision
- From: perkins at spot.colorado.edu (Tyler Perkins)
- Date: Fri, 6 May 1994 12:00:04 -0600
My thanks go to Martin Reinders for discovering a bug in my sh script
"mktemp", which is needed by my script "sci2mma". I submitted these
scripts to Mathgroup this past January.
Evidently, I assumed that the Unix "date" utility would return the day of
the month as, say, "05" (as it does with the time) instead of " 5". This
unexpected leading space character caused a space to appear in the midst of
the name returned by mktemp, which is to be used by sci2mma as a filename.
(Guess I never tested it during the first nine days of the month!)
Below is the corrected version of mktemp. The only change is in the
assignment "name=...". Note that the two sed commands (s/...) are
separated by a newline.
Tyler Perkins perkins at colorado.edu
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= cut here =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#!/bin/sh
# NAME
# mktemp - Create a unique filename.
#
# SYNOPSIS
# mktemp [ -c ] [ -d directory_name ] [ -p the_prefix ]
#
# DESCRIPTION
# The mktemp command makes a name for the pathname of a temporaryp
# returns the string "/tmp/username.DDHHMMSSP...P" where username
# is the user's username, DD is the day of the month, HH is the
# hour of the day (locally), MM is the minute of the hour, SS is
# the second of the minute, and P...P is the process id of the
# user's shell.
#
# OPTIONS
#
# -c
# Create an empty file whose filename is the generated name.
#
# -d directory_name
# Force directory_name to be used as the directory portion of
# the pathname. If this option is absent, /tmp is used by
# default.
#
# -p the_prefix
# Use string the_prefix as the filename's prefix. If this option
# is absent, the user's username is used by default.
#
# AUTHOR
# Tyler Perkins perkins at colorado.edu
# Boulder, CO
dir="/tmp"
prfx="$USER"
create=false
while [ "$1" != "" ]
do case $1 in
-d) shift
dir="`echo $1 | sed 's!\(.*\)/$!\1!'`"
shift
continue
;;
-p) shift
prfx=$1
shift
continue
;;
-c) shift
create=true
continue
;;
*) echo 'mktemp usage: mktemp [-d directory] [-p prefix] [-c]'
exit 1
;;
esac
done
name="$dir/$prfx.`date | sed 's/[^0-9]//g
s/\(.*\)..../\1/'`$$"
# If file with name $name already exists, recurse until not.
if [ -f $name -o -d $name ]
then name="`mktemp`"
fi
if $create
then >$name
else echo $name
fi