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


AIXtm mtXIA Technical Consulting Group

 


mkrandstr

- Korn shell script that generates a random string of characters of a variable length (configurable).


Current:mkrandstr
Previous:.
Home Page



#!/usr/bin/ksh93
################################################################
function usagemsg_mkrandstr {
  print ""
  print "Program: mkrandstr"
  print ""
  print "This script generates a random string of characters"
  print ""
  print "Usage: ${1##*/} [-?] [-v] [-V] [-m minimum] [-x maximum]"
  print ""
  print "    Where '-m minimum' = Specify the minimum number of characters"
  print "                 to be returned in the random string (Default:10)"
  print "          '-x maximum' = Specify the maximum number of characters"
  print "                 to be returned in the random string (Default:10)"
  print "          '-l' = Only return lower case characters in random string"
  print "          '-u' = Only return upper case characters in random string"
  print "          '-v' = Verbose mode"
  print "          '-V' = Very Verbose Mode"
  print ""
  print "Author: Dana French (dfrench@mtxia.com)"
  print ""
  print "\"AutoContent\" enabled"
  print ""
}
################################################################
function mkrandstr {
  typeset TRUE="1"
  typeset FALSE="0"
  typeset EXITCODE="0"
  typeset VERBOSE="${FALSE}"
  typeset VERYVERB="${FALSE}"
  typeset MIN="10"
  typeset MAX="${MIN}"
  typeset MAXLEN="10"
  typeset POSCNT
  typeset RANDSTR
  typeset CNUM="52"
  typeset CHARS
  CHARS=( a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z )
  
  while getopts ":vVulm:x:" OPTION
  do
      case "${OPTION}" in
          'v') VERBOSE="${TRUE}";;
          'V') VERYVERB="${TRUE}";;
          'm') MIN="${OPTARG}";;
          'x') MAX="${OPTARG}";;
          'u') typeset -u CHARS;;
          'l') typeset -l CHARS;;
          '?') usagemsg_mkrandstr "${0}" && exit 1 ;;
      esac
  done
   
  shift $(( ${OPTIND} - 1 ))
  
  trap "usagemsg_mkrandstr ${0}" EXIT
  if (( MAX < MIN ))
  then
    print -u 2 "ERROR: MIN (${MIN}) is greater than MAX (${MAX})"
    exit 2
  fi
  trap "-" EXIT
  
  (( VERYVERB == TRUE )) && set -x
  
################################################################
  
  (( VERBOSE == TRUE )) && print -u 2 "Min length of random string: ${MIN}"
  (( VERBOSE == TRUE )) && print -u 2 "Max length of random string: ${MAX}"

#### 
#### Calculate a random length for the random string between the
#### MIN and MAX values provided
#### 

  (( MAX != MIN )) && (( MAX++ ))
  (( MIN == MAX )) &&
    MAXLEN=${MIN} ||
    MAXLEN=$(( MIN + ( RANDOM % ( MAX - MIN ) ) ))

  (( VERBOSE == TRUE )) && print -u 2 "Act length of random string: ${MAXLEN}"
  
#### 
#### extract a random character from the CHARS array for each position
#### of the random string. The length of the random string determined by
#### the MAXLEN variable.
#### 

  for (( POSCNT=0; POSCNT < MAXLEN; ++POSCNT ))
  do

#### 
#### Using the modulo of a random number divided by the number of 
#### characters (array elements) in the CHARS array, provides a
#### random array element position.  The random array element position
#### is used to append a single character at a time from the CHARS array
#### onto the random string variable RANDSTR.
#### 

      RANDSTR="${RANDSTR}${CHARS[${RANDOM}%${CNUM}]}"
      (( VERBOSE == TRUE )) && print -u 2 "Intermediate value ($(( ${POSCNT} + 1 ))) of random string: ${RANDSTR}"

  done
  print ${RANDSTR}
  return ${EXITCODE}
}
################################################################

mkrandstr "${@}"

 

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