0

I'm trying to create users by importing their data from a .csv file by using a bash script. I know basics for bash but I'm getting confused using loops to create this script. The template for the .csv is the following:

Surname,Name,Username,Department,Password
jesse_b
  • 37,005
  • 5
    What code have you tried? This is not a code writing site it is a code review site. –  Jan 02 '18 at 18:53
  • I'm trying to use while loops mainly from examples I found on the internet but can't really understand them – Carl Carl Jan 02 '18 at 19:04
  • A description ("while loops mainly from examples") is something, but the exact code you've tried, along with how the output differs from your expectations, is much more useful. If you're having trouble with shell loops in general, note that this can be done with no loops at all. Also, is this a real CSV? Specifically, what happens if the password field (or any other field) contains a comma? – Fox Jan 03 '18 at 05:40

1 Answers1

1

This should work for you or at least help you get in the right direction:

#! /usr/bin/env bash -
#set -x

MY_INPUT='/root/temp/input'
declare -a A_SURNAME
declare -a A_NAME
declare -a A_USERNAME
declare -a A_DEPARTMENT
declare -a A_PASSWORD
while IFS=, read -r COL1 COL2 COL3 COL4 COL5 TRASH; do
    A_SURNAME+=("$COL1")
    A_NAME+=("$COL2")
    A_USERNAME+=("$COL3")
    A_DEPARTMENT+=("$COL4")
    A_PASSWORD+=("$COL5")
done <"$MY_INPUT"

for index in "${!A_USERNAME[@]}"; do
    useradd -g "${A_DEPARTMENT[$index]}" -d "/home/${A_USERNAME[$index]}" -s /bin/bash -p "$(echo "${A_PASSWORD[$index]}" | openssl passwd -1 -stdin)" "${A_USERNAME[$index]}"
done

The while loop will read through your csv file and create arrays for each column. the TRASH value is there to collect any additional fields that may be in your csv file. Otherwise if you had this for example: surname,name,username,department,password,anextrafield

COL5 would be set to: passwordanextrafield

Regardless, I think setting the arrays may not be necessary for you (as you could likely just create your users inside the while loop) but I wanted to show this way I've learned to work with looping through a delimited file.

After the for loop will run through the index values of the A_USERNAME array (It could have been any of the arrays) and act on them to create users.

useradd

  1. -g "${A_DEPARTMENT[$index]}"
    • This will add the newly created user to a group named after their department (note: this group must exist already or the command will error)
  2. -d "/home/${A_USERNAME[$index]}"
    • This will set the home directory for the newly created user to /home/[USERNAME]
  3. -s /bin/bash
    • Set the login shell of the newly created user to /bin/bash
  4. -p "$(echo "${A_PASSWORD[$index]}" | openssl passwd -1 -stdin)"
    • Takes the password from your csv and uses openssl to generate an MD5 password with it
  5. "${A_USERNAME[$index]}"
    • The user to create.
jesse_b
  • 37,005
  • Thanks this is really helpful. All users were created in the right group. However the password doesn't seem to be working. Tried copying-pasting from the csv file into the login screen but it still gets denied. – Carl Carl Jan 02 '18 at 23:06
  • @LinuxUSer, try removing the comment (#) in front of set -x and that will allow you to view your script more verbosely. You can see how the shell is interpreting that line and maybe it will point to why it's choking. – jesse_b Jan 03 '18 at 12:40