Linux:
Basic script to create users and groups from a text file.
martin,pier,1995-11-07,sales
silvie,audain,1975-10-08,HR
jacque,lamie,1988-09-09,tech
mike,paul,1989-08-10,admin
paul,simon,1976-07-12,sales
As you can see, each line contains the surname, the first name, the date of birth, the department and they all separated by a comma.
Script
ADDB=0 # initialize a variable
while IFS= :$’,’ read SURNAME FIRSTNAME DATE_OF_BIRTH DEPARTMENT # read each entry separated by a comma
do
ADDB=$ADDB+1
PASS[ADDB]=${DATE_OF_BIRTH:0:4} #memorized the password in the table
GROUP[ADDB]=$DEPARTMENT #memorizes the groups in the table at position ADDB
USER[ADDB]=$SURNAME${FIRSTNAME:0:1} #memorized the user (name + first letter first name)
Done < $1
### loop : add group
for ((x=1 ; x<$ADDB ; x++))
do
if grep “^${GROUP[x]}” /etc/group #analyze if the group exists in / etc / GROUP
then
echo – # if there is nothing to do
else
addgroup ${GROUP[x]} # if not exist add the group
fi
done
###loop : add user and password
for ((x=1 ; x<=$ADDB ; x++))
do
if grep –q ${USER[x]} /etc/passwd #analyze if the user exists in the file / etc / passwd
then
echo – #si oui ne rien faire
else
useradd –m –s /bin/bash –G ${GROUP[x]} ${USERR[x]} # if not add the user to his group
echo ${USER[x]}:${PASS[x]} | chpasswd # if not add the password to the user
fi
done
Sources : Audain.net