Chris Hupman
Chris Hupman

Reputation: 408

Adding users to linux box from LDAP

I received a request to add around 100 users to a linux box the users are already in ldap so I can't just use newusers and point it at a text file. Another admin is taking care of the ldap piece so all I have to do is create all the home directories and chown them to the correct user once he adds the users to the box. creating the directories isn't a problem, but I'd like a more elegant script for chowning them to the correct user. what I have currently basically looks like

chown -R testuser1 testgroup1 /home/tetsuser1; chown -R testuser2 testgroup2 /home/testgroup2; chown -R testsuser3 testgroup1 /home/testuser3

bascially I took the request that the user name and group name popped it into excel added a column of "chown -R" to the front, then added a column of "/", copied and pasted the username column after it and then added a column of ";" and dragged it down to the second to last row. Popped it into notepad ran some quick find and replaces and in less than a minute I have a completed request and a sad empty feeling. I know this was a really ghetto method and I'm trying to get away from using excel to avoid learning new scripting techniques so here's my real question.

tl;dr I made 100 home directories and chowned them to the correct users, but it was ugly. Actual question below.

You have a file named idlist that looks like this (only with say 1000 users and real usernames and groups)

testuser1 testgroup1
testuser2 testgroup2
testuser3 testgroup1

write a script that creates home directories for all the users and chowns the created directories to the correct user and group. To make the directories I used the following(feel free to flame/correct me on this as well. )

var= 'cut -f1 -d" " idlist' (I used backticks not apostrophes around the cut command)
mkdir $var

Upvotes: 3

Views: 661

Answers (3)

Dennis Williamson
Dennis Williamson

Reputation: 360675

#!/bin/bash
# should work in any POSIX shell
while read user group
do
    mkdir "/home/$user"
    chown "$user:$group" "/home/$user"
done < idlist

Upvotes: 3

JUST MY correct OPINION
JUST MY correct OPINION

Reputation: 36107

awk '{ system("mkdir " $1); system("chown " $1 ":" $2 " " $1); }' idlist

Tested just now for functionality. Needs to be run under root privileges of course. This doesn't assemble commands for you to be executed under backticks or $(). It executes them directly.

Edited to add:

You probably want to set that one-liner up in a script file or as an alias if this is something you do often. I do suggest, however, that you learn awk. It's a very nice little language and is actually guaranteed to be on any Posix-compliant system. (I think it's the only one other than sh, in fact.)

Upvotes: 1

Michael Mrozek
Michael Mrozek

Reputation: 175705

Using cut isn't bad; you could use it to assemble the whole command. You can also use awk, which is probably easier:

awk '{print "mkdir /home/" $1 "; chown -R " $1 ":" $2 " /home/" $1}' idlist

Upvotes: 2

Related Questions