We help IT Professionals succeed at work.

Create users from .csv - Mac OS X

masaiming
masaiming asked
on
I have a bunch of employees that I need to create accounts for.  I am very inexperienced with scripting.  

I have their user account, first name, last name, email address in columns.  This is saved as a .csv file.  

If someone could show me a template or example of where I can get this information, I would greatly appreciate it.  Thank you

Comment
Watch Question

Author

Commented:
This is using Open Directory on Mac OS X Lion.  
CERTIFIED EXPERT

Commented:
All you need is a simple shell script.  You need a loop that parses through the file and creates users.

Here is how you create users in Mac OS X
http://serverfault.com/questions/20702/how-do-i-create-user-accounts-from-the-terminal-in-mac-os-x-10-5


If you want to get it parsed, provide a sample csv file.

Author

Commented:
How do I have it generate a new UID every time it creates a new account?

So I just want to create their username, real name, login shell, unique id, home directory

I would create a .csv file with 5 columns with that information.

I running that single command is fine, but how do I loop pulling that information off the .csv file?
CERTIFIED EXPERT
Commented:
If you have a csv file then here is a perl script to parse through it.
use warnings;
use strict;

my $filename = 'ids.csv';
my $filehandle;

open ($filehandle, $filename) || die "Could not open file : $!\n";
while(<$filehandle>)
{
   my @fields = m{(\w+)}g;
   #here you can make system call
   #just to show the parsed output
   print "Field1 = " . $fields[0];
}

Open in new window

Commented:
You can just import the users using Workgroup Manager. Server>import.  It will automatically create the accounts and give you mapping options for many other attributes. It will create the UID for you for each account.