-1

I have a username.txt which contains their username and specific group. As of right now i'm trying to create a bash script which allows me to create an account for each user and add the user to its group in one command.

this is currently my failed bash script(i know pretty much everything is wrong but i hope you guys got a clear idea on it):

#!/bin/bash

sudo addgroup staff
sudo addgroup visitors

username="username.txt"

while read line; do
sudo useradd $-Eo '^[^,]+' $username;
if [grep staff $username]; then
  sudo usermod -a -G staff
else
  sudo usermod -a -G visitors
done < $username

This is what is inside my username.txt file:

ellipsiscoterie,visitor
magnetcommonest,visitor
belateddefensive,staff
bobstercaramelize,staff
Kinja
  • 449
  • 5
  • 22
  • Possible duplicate of https://stackoverflow.com/questions/64366569/linux-bash-read-each-word-seperated-by-comma ? – KamilCuk Oct 16 '20 at 08:27
  • 1
    I'm voting to close this as "needs more focus" because there is a large number of bugs in your code, many of which would be trivially reported by a simple check like http://shellcheck.net/ -- in the future, please review the guidance for providing a [mre] of one specific, repeatable problem. – tripleee Oct 16 '20 at 08:37

3 Answers3

1

You can use awk to write it in one line. In this case, awk splits each row into different columns and you can access each field separately.

awk -F "," '{ if(system("grep -q -E "$2" /etc/group") != 0 ){system("groupadd "$2)}; system("useradd "$1" -G "$2)}' username.txt

The first argument (-F ",") defines the field-separator, but it could be also something else e.g. ";" or "/"

The part if(system("grep -q -E "$2" /etc/group") != 0 ) verifies if the group exist and if not, the part {system("groupadd "$2)} creates the group before the next command system("useradd "$1" -G "$2) creates the user and adds it to the group $2.

It's possible to simplify the command to remove the if part, but then you will get a warning message that the user already exists.

awk -F "," '{ system("groupadd "$2); system("useradd "$1" -G "$2)}' username.txt
groupadd: group 'visitor' already exists
groupadd: group 'staff' already exists

btw: the part system executes just an operating system command

aalbagarcia
  • 1,019
  • 7
  • 20
Kalle
  • 11
  • 2
1

Let's go through your script.

#!/bin/bash

sudo addgroup staff
sudo addgroup visitors

username="username.txt"

OK. There is some debate about using sudo in scripts, but I'm not against it.

while read line; do

You read the line from STDIN, which is your input file. The variable line contains ellipsiscoterie,visitor in the first iteration.

sudo useradd $-Eo '^[^,]+' $username;

$- prints The current set of options in your current shell. It will produce something like himBH. The next argument seems a regular expression, and the last argument is the filename that you use. So the command here is:

sudo useradd himBHEo '^[^,]+' username.txt

Hint: if you are unsure of the arguments, check with an echo (echo $-Eo '^[^,]+' $username) before you add them to a sudo-ed command.

This is not what you want. First, you probably want to use the variable line instead of username. Why would you otherwise loop through that file?

Second, read-up on variable expansion in bash. For now, try:

line=ellipsiscoterie,visitor
echo ${line%,*}
echo ${line#*,}

So the line would probably need to be:

sudo useradd ${line%,*}
if [grep staff $username]; then

This is wrong in almost everything.

  • the [ and ] require spaces to set them apart
  • but you dont want to do a test, you want to see if the grep succeeds, so the [ and ] are not needed anyway
  • you are again using the complete file. So the grep succeeds if there is any line with staff in it; even if the username would be johnfalstaff

What you really want to know is if the second column in your line is staff, so:

if [ "${line#*,}" = "staff" ] ; then
  sudo usermod -a -G staff
else
  sudo usermod -a -G visitors

So, where is the fi that closes the if statement?

done < $username

Also, quote the filename: done < "$username"

tripleee
  • 175,061
  • 34
  • 275
  • 318
Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
0

The newusers command allows you to configure users from a file in batch:

https://www.man7.org/linux/man-pages/man8/newusers.8.html

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18