tags:

views:

112

answers:

3

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

+2  A: 

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
Michael Mrozek
Yeah, awk would be a better fit for this job I'd think.
JUST MY correct OPINION
+1  A: 
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.)

JUST MY correct OPINION
despite answering second I choose this as the correct answer after testing both scripts. I was looking at awk, but I was having issues with the syntax and ended up saying screw it and reverting to bad habits to close the ticket after I couldn't figure out how to get it to read from the file. Now that I have a working example I'll go look at where I was failing tomorrow and I can feel more comfortable studying the man pages now that I have a known good to work with. I appreciate the answer and I promise to hit the books and try to do better next time :).
maxxpower
Awk is one of the better tools in a system administrator's belt. It's great for massaging log files and for automating any kind of task that involves extracting data from (perhaps loosely) formatted text without bringing in the heavyweight executable line noise that is Perl. I'd recommend picking up the [GNU book on using AWK effectively](http://www.gnu.org/software/gawk/manual/) and giving it a read over. Lots of good information in there.
JUST MY correct OPINION
+3  A: 
#!/bin/bash
# should work in any POSIX shell
while read user group
do
    mkdir "/home/$user"
    chown "$user:$group" "/home/$user"
done < idlist
Dennis Williamson
so just to make sure I understand what's going on I thought I'd go over what I think this is doing. you're starting a loop telling it to read from stdin and assign the first column as $user and the second column and $group. < idlist is telling the script to use the idlist as the stdin. It runs though the commands with the assigned variables until it reaches the end of file. Very cool and thank you sir.
maxxpower
@maxxpower: Yes, that's what it's doing.
Dennis Williamson