0

So, I have a text file that is organized like this

<username>:<fullname>:<usergroups>

I need to create a new user for each line and put them into their groups. I am stuck with trying to set username into a variable to use with useradd. I have tried using cut but it needs a file name, I can't just pass it a line.

Here is what I currently have:

#! /bin/bash
linesNum=1 
while read line
do
  echo
  name=$( cut -d ":" -f1 $( line ) )
  ((lineNum+=1))
done < "users.txt"

Thanks for your help!

1
  • $(line) is really wrong, it will consider line as a command, what you want is rather "$line" or "${line}". Commented Nov 5, 2010 at 9:18

2 Answers 2

3
#!/bin/bash
while IFS=: read username fullname usergroups
do
    useradd -G $usergroups -c "$fullname" $username
done < users.txt

fullname is the only string that should contains whitespace (hence the quotes), A list of usergroups should be separated from the next by a comma, with no intervening whitespace (so no quotes on that argument) and your username should not contain whitespace either.

Upate:

To get the list of usergroups to create first you can do this...

#!/bin/bash
while IFS=: read username fullname usergroups
do
    echo $usergroups >> allgrouplist.txt
done < users.txt

while IFS=, read group
do
   echo $group >> groups.txt
done < allgrouplist.txt

sort -u groups.txt | while read group
do
   groupadd $group
done

This is a bit long winded, and could be compacted to avoid the use of the additional files allgrouplist.txt and groups.txt but I wanted to make this easy to read. For reference here's a more compact version.

sort -u < $(
       echo $(while IFS=: read a b groups; do echo $groups; done < users.txt )
      | while IFS=, read group; do echo $group; done ) 
| while read group
do
       groupadd $group
done

(I screwed the compact version up a bit at first, it should be fine now, but please note I haven't tested this!)

Sign up to request clarification or add additional context in comments.

6 Comments

Is there a way to do this without adding the user groups before hand? Could I loop through usergroups and create each one before I do useradd? Also: What exactly does IFS=: do? Does it set the field seperator to : ?
@amazinghorse24 the bash reference explains the special variables :-)
IFS is the standard field separator environment var. You could do the group adding as a separate operation yes, use usermod -g $usergroups $username.
@amazinghorse: $IFS is the input field separator; it's used by the shell to split the input for various things, such as read. You could do a similar thing with the value of $usergroups, except you pass -d ',' to read to split by commas instead of newlines.
I'll update the answer to show how you'd add all the usergroups.
|
1
IFS=: while read username fullname usergroups
do
  useradd -G "$usergroups" -c "$fullname" "$username"
done < users.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.