mkuid

Korn Shell mkuid: Generate Enterprise Wide unique UID/GID number/values for usernames and group names




################################################################
#### 
#### This Korn Shell 93 function generates a uniq UID number/value
#### for a username, that username must consist of 3 letters 
#### followed by 4 digits, or 4 letters followed by 3 digits.  The 
#### username is assumed to be the first argument to the function. 
#### The username must also be exactly 7 characters long.
#### 
#### This function is dependent upon the external 
#### function "mkascii".
#### 
################################################################
function mkuid
{
  typeset -l MKUID_LET
  typeset MKUID_NUMLET=0
  
  MKUID_NUMLET="${1//([!a-zA-Z])/}"
  MKUID_NUMLET="${#MKUID_NUMLET}"

  if (( ${#1} != 7 ))
  then
      print -u 2 "ERROR: Number of characters in username ${1} is not equal to 7\n"
      return -1
  fi
  
  if (( MKUID_NUMLET != 3 )) && (( MKUID_NUMLET != 4 ))
  then
      print -u 2 "ERROR: Number of letters in username ${1} is not equal to 3 or 4\n"
      return -1
  fi
  
  (( MKUID_TYPE = 10 ** ( 7 - MKUID_NUMLET ) ))
  typeset MKUID_LOWLIM=1000000
  (( MKUID_LOWUID = ( 26 ** ( MKUID_NUMLET - 1 ) * MKUID_LOWLIM / 100 ) + MKUID_LOWLIM ))
  (( MKUID_NUMLET == 3 )) && (( MKUID_LOWUID = MKUID_LOWLIM ))
  
  typeset MKUID_BASE=26
  typeset MKUID_NVALUE=""
  typeset MKUID_LVALUE=0
  typeset MKUID_ECNT=0
  typeset MKUID_SUBTOT=0
  typeset MKUID_TOT=0
  typeset MKUID_MULT=0
  
  typeset -A MKUID_LETTERS
  mkascii MKUID_LETTERS
  
  # print " ${MKUID_LETTERS[@]}"
  
  eval EACHLET="( ${1//(?)/ \1} )"
  for MKUID_LET in "${EACHLET[@]}"
  do
  
    if [[ "_${MKUID_LET}" = _[a-z] ]]
    then
      MKUID_LVALUE="$(( ${MKUID_LETTERS[${MKUID_LET}]} - 97 ))"
    else
      MKUID_LVALUE="0"
      MKUID_NVALUE="${MKUID_NVALUE}${MKUID_LET}"
    fi
  
    (( MKUID_MULT = MKUID_BASE ** MKUID_ECNT ))
    (( MKUID_SUBTOT = MKUID_SUBTOT + MKUID_LVALUE * MKUID_MULT ))
    (( MKUID_ECNT = MKUID_ECNT + 1 ))
  done
  
  (( MKUID_TOT = ( ( MKUID_SUBTOT * MKUID_TYPE ) + MKUID_NVALUE ) + MKUID_LOWUID ))
  
  print ${MKUID_TOT}
  return 0
}