DEV Community

learneriq
learneriq

Posted on

Write A script to create users from csv file.

Here is the code from which you can create users from csv file .





#!/bin/bash
group=`awk -F, '{print $2}' users.csv `
groups=`echo $group | sed -e 's/\s\+/,/g'`
while IFS=, read -r username group shell homedir status; do
        groupadd  $group
        password=`date +%s | sha256sum | base64 | head -c 32` 

        useradd ${username}  -m -d /home/${username} -s ${shell} -g ${group} -p ${password}

        chmod -R 774 /home/${username} #3 ) 

        chage --lastday 0 ${username} 
        echo -e "${username},${password}" >> cred.txt 

        usermod -aG ${groups} ${username}

        if [[ ${status} == "N" ]]
        then
                userdel -r ${username}
        else
                echo "do nothing "
        fi
done < users.csv  


Top comments (1)

Collapse
 
aswad1979 profile image
F-ASWAD

Hello,

Is the csv file has only users names?
How to execute the script? do I need to provide arrangements like a group name?

Thx!