How to do it...

Now, we will create a script to add a user. The useradd command is used to create a user. We are going to use the while loop, which will read our .csv file, and we will use the for loop to add each user that's present in that .csv file.

Create a script using add_user.sh:

#!/bin/bash#set -xMY_INPUT='/home/mansijoshi/Desktop'declare -a SURNAMEdeclare -a NAMEdeclare -a USERNAMEdeclare -a DEPARTMENTdeclare -a PASSWORDwhile IFS=, read -r COL1 COL2 COL3 COL4 COL5 TRASH;do    SURNAME+=("$COL1")    NAME+=("$COL2")    USERNAME+=("$COL3")    DEPARTMENT+=("$COL4")    PASSWORD+=("$COL5")done <"$MY_INPUT"for index in "${!USERNAME[@]}"; do useradd -g "${DEPARTMENT[$index]}" -d "/home/${USERNAME[$index]}" -s /bin/bash -p "$(echo "${PASSWORD[$index]}" ...

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.