0

As of right now, I'm trying to reach each word, with the first word being useradd and the second word being groupadd, but I'm having a bit of trouble with the ","

This is my username.txt file:

jason,staff
henson,visitor

This is my current bash script:

#!/bin/bash

username="username2.txt"

while read username group; do
  sudo useradd $username;
  sudo groupadd $group;
done < $username
tripleee
  • 175,061
  • 34
  • 275
  • 318
Kinja
  • 449
  • 5
  • 22

1 Answers1

2

Change the field separator IFS to comma.

filename="username2.txt"
while IFS=, read -r username group
do
    sudo useradd "$username"
    sudo groupadd "$group"
done < "$filename"

And don't reuse the variable $username for both the file and your iteration variable.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • btw erm, any idea how i can make their initial password the same as their username? – Kinja Oct 15 '20 at 07:30
  • See https://stackoverflow.com/questions/2150882/how-to-automatically-add-user-account-and-password-with-a-bash-script – Barmar Oct 15 '20 at 07:34