100% Spam Free Email | Contact Us | Products/Services | Korn Shell | MicroEMACS


Mt Xia:
Technical Consulting Group

 

mktemp_k93


Contact Mt Xia for help with your shell scripting projects at 615.556.0456 or dfrench@mtxia.com

Current:mktemp_k93
Previous:
Home Page



#!/usr/bin/ksh93
################################################################
function usagemsg_mktemp_k93 {
  print "
Program: mktemp_k93

This script generates a random temporary file and returns
the name of the generated file to standard output.

Usage: ${1##*/} [-?] [-v] [-V] [-d DIRECTORY]

    Where:
          '-d' = Directory in which to create the temporary file
          '-v' = Verbose mode
          '-V' = Very Verbose Mode

Author: Dana French (dfrench@mtxia.com)    Copyright 2004

\"AutoContent\" enabled"
return 0
}
################################################################
function mktemp_k93 {
  typeset TRUE="0"
  typeset FALSE="1"
  typeset VERBOSE="${FALSE}"
  typeset VERYVERB="${FALSE}"
  typeset DD_TMP="/tmp"
  typeset EXITCODE="0"
  
  while getopts ":d:vV" OPTION
  do
      case "${OPTION}" in
          'd') DD_TMP="${OPTARG}";;
          'v') VERBOSE="${TRUE}";;
          'V') VERYVERB="${TRUE}";;
          '?') usagemsg_mktemp_k93 "${0}" && return 1;;
      esac
  done
   
  shift $(( ${OPTIND} - 1 ))
  
  if [[ ! -d "${DD_TMP}" ]]
  then
      print -u 2 "# ERROR: Invalid Directory: ${DD_TMP}"
      return 2
  fi

  (( VERYVERB == TRUE )) && set -x
  
################################################################
  
#### 
#### Generate a random number at least 6 characters long
#### and assign it to a variable that is exactly 6 characters
#### long.  Build a temporary file name from this 6
#### character variable using the default temporary
#### directory, or a user specified directory.
#### 

  typeset -L6 RAND="${RANDOM}${RANDOM}${RANDOM}${RANDOM}${RANDOM}${RANDOM}"
  TEMPFILE="${DD_TMP%/}/temp.${RAND}"

  (( VERBOSE == TRUE )) && print -u 2 "# Generated Random Number: ${RAND}"

#### Check to see if a file already exists with this full
#### path file name, if so, regenerate the 6 character random
#### number and retest.  Repeat this process until a file
#### name is found that does not already exist.

  until [[ ! -f ${TEMPFILE} ]]
  do
     (( VERBOSE == TRUE )) && print -u 2 "# ${TEMPFILE} already exists, retrying"

     typeset -L6 RAND="${RANDOM}${RANDOM}${RANDOM}${RANDOM}${RANDOM}${RANDOM}"
     TEMPFILE="${DD_TMP%/}/temp.${RAND}"

     (( VERBOSE == TRUE )) && print -u 2 "# Generated Random Number: ${RAND}"
  done

#### Create an empty file using this randomly generated file
#### name.  Set the umask to create the file with 600 permissions,
#### meaning the owner has read-write permission, all other
#### permissions are denied.  Reset the umask back to its
#### orignal setting after the temporary file is created.

  (( VERBOSE == TRUE )) && print -u 2 "# Creating ${TEMPFILE}"

  UMASK=$( umask )
  umask 0077
  if ! exec 3>"${TEMPFILE}"
  then
      print -u 2 "# ERROR: Unable to create temporary file"
      EXITCODE="3"    
  fi
  exec 3>&-
  umask ${UMASK}

#### Print the file name to standard output so the calling
#### program will know the name of the randomly generated
#### temporary file.

  print -- "${TEMPFILE}"

#### Return to the calling function with a successful return code.

  return ${EXITCODE}

}
################################################################

mktemp_k93 "${@}"

 

For information regarding this page, contact Dana French ( dfrench@mtxia.com )
Copyright 2008 by Mt Xia Inc, All Rights Reserved