HOWTO: Bash Scripting: add users, groups, passwords from csv file

In this assignment we were given a csv file that contained a first name, last name, department, id #, group1, group2, group3, group4 and group5. We were then required to write a script that would complete the following tasks.
1. Create a user name from the last name and first inital.
2. Create a comment that would be their first and last name.
3. Create a unique password for each user.
4. Put each user in their given groups.
5. Send them an email with user name and password.

Here is the script to create the users and to delete the users if it is a test machine.

Add User Script

#!/bin/bash
#Kyle Corey, March 3rd 2008, Version 1.012.12
#This script takes a csv file with users and thier info and makes users, groups, passwords and sends an email

#checks to see if user is root
if [ "$(whoami)" != "root" ]
then
echo "Error: You ARE NOT ROOT!!!!!"
exit 1
else
echo "You are Lucky! I am watching you!!"
echo "Please enter the CSV"
#checks to see if file exsits
read CSVFILE
if [ -f $CSVFILE ]
then
exec < $CSVFILE
#while loop to check and manipulate each line
while
read line
do
#cuts the first name from csv
firstName=$(echo $line | awk -F, '{print $1'})
#cuts the last name from csv
lastName=$(echo $line | awk -F, '{print $2'})
#cuts students for student group
department=$(echo $line | awk -F, '{print $3'})
#changes all capital letters to small so Year One would become year one
department=$(echo $department | tr 'A-Z' 'a-z')
#removes any spaces in student so year one would be yearone
department=$(echo $department | sed 's/ //')
#cuts the 5 groups from the csv
group1=$(echo $line | awk -F, '{print $5'})
group2=$(echo $line | awk -F, '{print $6'})
group3=$(echo $line | awk -F, '{print $7'})
group4=$(echo $line | awk -F, '{print $8'})
group5=$(echo $line | awk -F, '{print $9'})
#cuts the first initial from the first name
firstInit=$(echo $line | cut -c1)
#puts last name with first inital to create user name ie coreyk
userName=$lastName$firstInit
#changes entire user name to small letters
userName=$(echo $userName | tr 'A-Z' 'a-z')
#makes first letter of first name capital
firstInit=$(echo $firstInit | tr 'a-z' 'A-Z')
#cuts the first inital off of first name off the first name
fNameCut=$(echo $firstName | cut -c2-40)
#cuts first letter of last name
lastInit=$(echo $lastName | cut -c1)
#makes the first letter of the last name capital
lastInit=$(echo $lastInit | tr 'a-z' 'A-Z')
#cuts the first inital of the last name off the last name
lNameCut=$(echo $lastName | cut -c2-40)
#creates date variables
day=$(date +%d)
month=$(date +%m)
year=$(date +%Y)
#add one year for next
nextYear=$((year + 1))
#creates the groups by removing the comma's so I can add the different groups each user is in
groups=$(echo "$group1","$group2","$group3","$group4","$group5")
groups=$(echo ${groups#',,,,'})
groups=$(echo ${groups#',,,'})
groups=$(echo ${groups#',,'})
groups=$(echo ${groups#','})
groups=$(echo ${groups%',,,,'})
groups=$(echo ${groups%',,,'})
groups=$(echo ${groups%',,'})
groups=$(echo ${groups%','})
groups=$(echo $groups | tr 'a-z' 'A-Z')
#if statement that checks if the user is in yearone group and if it is it puts them \
#in student group and add the other groups
if [ "$department" = 'yearone' ];
then
students=$(echo 'students',"$groups")
else
students=$(echo "$groups")
fi
#adds user, a comment, expiry date and the groups
useradd "$userName" -c "$firstInit$fNameCut $lastInit$lNameCut" -e "$nextYear-$month-$day" -G "$students"
#echos the activity
echo "useradd $userName -c $firstInit$fNameCut $lastInit$lNameCut -e $nextYear-$month-$day -G $students"
#ranlist is a password matrix variable
ranlist="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#8 characters are created and put into a string for a unique 8 char password
passChar1=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar2=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar3=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar4=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar5=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar6=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar7=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar8=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
pass=$passChar1$passChar2$passChar3$passChar4$passChar5$passChar6$passChar7$passChar8
#assigns a password for the user
echo "$pass" | passwd --stdin "$userName"
#echos password for terminal display
echo "Password for $userName is $pass"
#emails username, password and other info to user and admin
subject="Welcome to ITAS"
to="$userName@itas.ca"
message="/tmp/emailmessage.txt"
echo "Welcome to ITAS $firstInit$fNameCut $lastInit$lNameCut" >> $message
echo "User Name: $userName" >> $message
echo "The password to your account is $pass" >> $message
echo "Your account expires on $nextYear-$month-$day" >> $message
echo "We recommend changing your password immediately" >> $message
echo "From your user account type passwd and new password" >> $message
echo "GOOD LUCK!!!" >> $message
/bin/mail -s "$subject" "$to" < $message
rm $message
echo "|~~~~Next~~~Next~~~~~~Next~~~~~~~Next~~~~~~~~Next~~~~~~~~Next~~~~~~Next~~~~~~|"
done
else
echo "Your Dead"
fi
fi

Delete User Script

#!/bin/bash

echo "Assignment1"
if [ "$(whoami)" != "root" ]
then
echo "Error: You ARE NOT ROOT!!!!!"
exit 1
else
echo "You are Lucky! I am watching you!!"
echo "Please enter the CSV"
read CSVFILE
exec < $CSVFILE
while
read line
do
firstName=$(echo $line | awk -F, '{print $1'})
lastName=$(echo $line | awk -F, '{print $2'})
idNumber=$(echo $line | awk -F, '{print $3'})
firstInit=$(echo $line | cut -c1)
userName=$lastName$firstInit
userName=$(echo $userName | tr 'A-Z' 'a-z')
userdel $userName
rm -rf /home/$userName
echo "Successfully deleted user $userName"
done
fi

Did this help or was it useful?? Are there errors??
Please post feed back so I can change them or know that I have helped.