2015-01-28

Linux Bash: creating employee codes from basic personal details

Few years ago I wrote a simple bash script to generate employee codes for future project management systems. Now, that the time has come to create a system that must be able to generate employee codes automatically, I realized that it is more elegant to generate employee codes directly with PL/SQL functions so the script has become completely useless. I was going to delete it permanently from my filesystem, but then checked the code and decided to post it here because it is a simple but perfect example of how fast problems can be solved using Linux bash only. Experts can skip this post because probably it will show nothing new for them.

The code itself

The script on my computer is called generatePID.sh and it is expecting 4 parameters, where the last one is optional: forename, surname, birthdate and creation date logging. When the value of the last parameter is 1 then it will print out the generated employee code with its creation date, otherwise nothing will be printed but the employee code.
You can run the script from terminal with the parameters described above. For example:
./generatePID.sh George Smith 1980.01.01
will print out
GESMIAABAB150128
and
./generatePID.sh George Smith 1980.01.01 1
will print out
GESMIAABAB150128
Generation date: 2015. jan. 28., wednesday, 19.13.16 CET

The code

#!/bin/bash
#PERSONAL ID GENERATOR SCRIPT
#HELP
if [ $# -lt 3 ]; then
echo "PERSONAL ID GENERATOR SCRIPT"
echo "Usage: $0 FORENAME SURNAME BIRTHDATE [0|1]"
echo "FORENAME: the forename of a specified person"
echo "SURNAME: the surname of a specified person"
echo "BIRTHDATE: the birthdate of a specified person, dot-separated"
echo "[0|1]: hide generation date (default) / show generation date"
echo "Examples:"
echo "George Smith 1980.01.01"
echo "George Smith 1980.01.01 0"
echo "George Smith 1980.01.01 1"
exit 1
fi
#CONVENTIONS
fnameMax=2
snameMax=2
#DEPENDING ON PERSONAL DETAILS
forename=$1; fnamePref=`echo ${forename:0:$fnameMax} | tr [a-z] [A-Z]`
surname=$2; snamePref=`echo ${surname:0:$snameMax} | tr [a-z] [A-Z]`
nameCode=$fnamePref$snamePref
birthdate=$3; bdateCode=`echo ${birthdate:2} | sed "s/[^0-9]//g" | tr [0-9] [A-J]`
#GENERATION DATE
dateCode=`date +%y%m%d | sed "s/[^0-9]//g"`
#RANDOM 4 DIGIT CODE
#randCode=$[ 1000 + $[ RANDOM % 9000 ] ]
#finalCode=$nameCode$bdateCode$randCode
#DISPLAY RESULT
finalCode=$nameCode$bdateCode$dateCode
echo $finalCode
#DISPLAY GENERATION DATE
if [ $# -ge 4 ]; then
if [ $4 -eq 1 ]; then
echo -n "Generation date: "
date
fi

fi

How it works

The first line tells the OS we want to use bash for the entire script. It's quite useful to start every Linux/Unix script by defining which shell we want to use to avoid unexpected results.
Everything else after the '#' character to the end of the line is handled as a comment and is skipped from processing.
The following line checks if less than 3 parameters are given and then the script will print out its help and exit:
if [ $# -lt 3 ]; then
The "echo" command will print out its arguments, "exit 1" will make sure the execution of the script is aborted with exit code 1.
"fnameMax" is the maximum length of the forename in the employee code. "snameMax" is the same with the surname.
The following commands will create a temporary variable for the forename, cut everything after the last allowed character from it and convert all characters to capitals:
forename=$1; fnamePref=`echo ${forename:0:$fnameMax} | tr [a-z] [A-Z]`
The same will apply to the surname. Then, the new temporary variables will be merged and that will be the name-code component of the employee code.
The following commands will create a temporary variable for the birthdate, cut the first 2 characters from it (making the year component a 2-digit code), delete everything from it leaving only numbers and convert the remaining numbers to letter-codes (capitals). The new temporary variable will be the birthdate-code component in the employee code.
birthdate=$3; bdateCode=`echo ${birthdate:2} | sed "s/[^0-9]//g" | tr [0-9] [A-J]`
Then we create the last component of the employee code from the current system date:
dateCode=`date +%y%m%d | sed "s/[^0-9]//g"`
Now we merge all new temporary variables in the correct order and that will be the employee code which will be printed out at the end of the script:
finalCode=$nameCode$bdateCode$dateCode
echo $finalCode
Lastly, we check the value of the last parameter if it was given. If its value is 1 then we print out the creation date:
echo -n "Generation date: "
date
("-n" will strip the end-of-line character in the echo command so the result of the "date" command will be printed out in the same line.)
There is a second version of the script commented out, which will generate random numbers as the last component of the employee code, but I never planned to use it actually to make sure that the generated codes will be unique.

The reason for deletion

A production environment should not call shell scripts for creating codes in a database since it is not considered being safe. Also, the uniqueness of these codes cannot be guaranteed when working with huge amount of data. This shell script does not deal with the unique database IDs of the records which are taking an important part in the final employee codes to make sure they are completely unique.
Anyway, the script can be useful when you are working only with a few records and you want to generate employee codes which are easy to remember.

No comments:

Post a Comment