Borne Shell Script – This is used for taking a formatted user list and adding users.

# /bin/bash
# Borne Shell Script
# This is used for taking a formatted user list and adding users. Run as root.
# Command line usage: this_script name_of_user_list_file
# Example line of file-input:”     200780958   AGARWAL, SAURABH”
# Example line of file-input:”     203660406   MASON, DOUGLAS E”
# Example line of file-input:”     203624328   MCCAULEY, MATTHEW J.”
# Example line of file-junk: ” DAYS  TR    PERIOD  D                  SEM HOURS =   3.0″
#
#GREP filters out the lines with 9 consecutive digits.
grep “[0-9]\{9\}” $1 > raw_uu_list;
#Change spaces,tabs,commas,and periods, all to dashes.
cat raw_uu_list | tr ‘ ‘ ‘-’ | tr ‘,’ ‘-’ | tr ‘.’ ‘-’ > uu_list_C;
#Trim the userlist so the junk displayed on the side is not there.
cat uu_list_C | cut -b 0-46 > uu_list_C_T;
#Add a “-x” onto every line, for anyone who does not have a middle name.
cat uu_list_C_T | awk -F : ‘{print $0″-X”;}’ > uu_list_C_T_X;
#Normalize columns by replacing the blocks of dashept name_of_user_list_file
# Example line of file-input:”     200780958   AGARWAL, SAURABH”
# Example line of file-input:”     203660406   MASON, DOUGLAS E”
# Example line ofPASSNG file-input:”     203624328   MCCAULEY, MATTHEW J.”
# Example line of file-junk: ” DAYS  TRs with a single “:”.
cat uu_list_C_T_X | sed “s/-*-/:/g” > uu_list_C_T_X_N;
#Edit file to have only the 4 used columns.
cat uu_list_C_T_X_N | awk -F : ‘{print $1$2″:”$3″:”$4″:”$5;}’ > uu_list_C_T_X_N_4C;
#Get the last 4 digits of the SS# in a file.
cat uu_list_C_T_X_N_4C | awk -F : ‘{print $1;}’ | cut -b 6-9 > SSN;
#Generate the user login-name and password.
cat uu_list_C_T_X_N_4C | awk -F : ‘{print $2;}’ | cut -b 0-1 > LIN;
cat uu_list_C_T_X_N_4C | awk -F : ‘{print $3;}’ | cut -b 0-1 > FIN;
cat uu_list_C_T_X_N_4C | awk -F : ‘{print $4;}’ | cut -b 0-1 > SIN;
paste -d :  FIN SIN LIN SSN | sed “s/://g” | tr “[A-Z]” “[a-z]” > LOGNTMP;
cat LOGNTMP | tr “[A-Z]” “[a-z]” > LOGNG;
cat LOGNG | rev > PASSNG;
#Generate formated user names.
cat uu_list_C_T_X_N_4C | awk -F : ‘{print $2;}’ | cut -b 2-12 | tr “[A-Z]” “[a-z]” > LP;
cat uu_list_C_T_X_N_4C | awk -F : ‘{print $3;}’ | cut -b 2-12 | tr “[A-Z]” “[a-z]” > FP;
paste -d “” FIN FP > FNAME;
paste -d “” LIN LP > LNAME;
paste -d ” ” FNAME SIN LNAME > NAME;
#Take out userlist names that already exist, echo errors,
#and add users, set passwords, and set directory permisions.
paste -d “_” LOGNG PASSNG NAME | sed “s/ /+/g”> Raw_ADDLST;
for i in `cat Raw_ADDLST`
do
username=`echo $i | sed “s/_/:/g” | awk -F : ‘{print $1}’`
current_user_exists=`grep -c $username “/etc/passwd”`;
if [ "$current_user_exists" = "1" ]
then
echo “User already on system: “$username
else
#Add users and set passwords and permisions…
username=`echo $i | sed “s/_/:/g” | sed “s/+/ /g” | awk -F : ‘{print $1}’`
password=`echo $i | sed “s/_/:/g” | sed “s/+/ /g” | awk -F : ‘{print $2}’`
fullname=`echo $i | sed “s/_/:/g” | sed “s/+/ /g” | awk -F : ‘{print $3}’`
echo $username”—”$fullname
useradd -m -c “$fullname” $username
echo $password | passwd –stdin $username
fi
done
#Clean up buckets of temp files.
rm -f raw_uu_list, uu_list_C, uu_list_C_T, uu_list_C_T_X, uu_list_C_T_X_N;
rm -f uu_list_C_T_X_N_4C, SSN, FIN, LIN, SIN, LOGNTMP, LOGNG, PASSNG, LP, FP;
rm -f FNAME, LNAME, NAME, Raw_ADDLST;

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>