1

I have 2 text files, users.txt with a list of 1000 users and groups.txt with a list of 50 groups. I want to run a command that adds 30 users to each group (ex: users 1-30 to group 1, users 31-60 to group 2, etc.). What would be the most practical way of doing this?

I suppose I could create 50 new text files with a list of 30 users on each and run 50 bash scripts, but I feel there is a better way than this.

Edit: This is what I have so far:

for i in `cat users.txt` ; do useradd $i; echo Pass$i | passwd $i -- stdin; done
for i in `cat groups.txt ; do groupadd $i; done
  • 1
    To do this programmatically you need a user/group mapping. ie. tom:office. Having 50 textfiles each representing one group with the groupname as filename and the contents one user per line could be one option.Give examples of users.txt, groups.txt and how the user/group relation is done between those files to get help. – Michael D. Jan 23 '19 at 15:17
  • 1
    @MichaelD. It's just an excercise, so the relation is not super important. The users are something like Joe, Sally, Bob, and the groups are something like Arizona, Florida, Utah. I just need each group have 30 users, but I don't have much experience in scripting in Linux. – greglorious_85 Jan 23 '19 at 15:25
2

Although it is just an exercise and you should do it by yourself, here is a solution: You really should do that by yourself, or be dumb.

Using some loops and simple tests:

while read group
do 
  i=30
  while read user
  do 
    echo $user:$group
    let "i--" 
    if [ $i -eq 0 ]
    then 
      break
    fi
  done < /tmp/users.txt
  sed -i -e '1,30d' /tmp/users.txt
done < groups.txt > userswithgroup.txt

Note that this will empty your users.txt file. so better make a copy.

Then you still have to add the users; but that's not complicated at this point.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.