LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-07-2005, 04:52 PM   #1
muppski
Member
 
Registered: Feb 2003
Posts: 149

Rep: Reputation: 15
Adding users


Hey there

I need a script to add all users from a exported excel dokument to text file with tabbed or comma seperated format.

I've got the following fields


Username Groupname firstname aftername dateofbirth city

Now I need to make the script
add a user with username and set the password as the same as the username
then add it to the groupname and add all that addinitnal info.

I'm not used with scripting and I dont know what language.(Bash will do?)
Could anyone help me out here?

I would really appreciate it
 
Old 04-07-2005, 05:45 PM   #2
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
you might man useradd. you could probably set up a script to run the command with all your parameters.
 
Old 04-08-2005, 12:00 AM   #3
muppski
Member
 
Registered: Feb 2003
Posts: 149

Original Poster
Rep: Reputation: 15
I known useradd and the different paramaters if I was running them as a command but I'm clueless how to make it a file that I can simply run that will add all these users (about 40)
 
Old 04-08-2005, 02:10 AM   #4
Optimistic
Member
 
Registered: Jun 2004
Location: Germany
Distribution: Debian (testing)
Posts: 276

Rep: Reputation: 33
A bash script should do the job, with a little help from awk, of course. It is way too many beers into the evening for me to give much help, but just look up some stuff on awk--it isn't too hard to pull info out of a cvs file and redirect it to a regular terminal command like adduser (not exactly sure on how to solve the whole new-password problem though). I'll look into more details for you tomorrow when the screen stops going all blury.
 
Old 04-08-2005, 02:34 AM   #5
DaHammer
Member
 
Registered: Oct 2003
Location: Planet Earth
Distribution: Slackware, LFS
Posts: 561

Rep: Reputation: 30
Here's something to get you started.
Code:
#!/bin/sh
# WARNING: USE AT YOUR OWN RISK!!!

FILE=test.txt              # Name of the file to parse
SHELL=/bin/bash            # Default shell
USERADD=/usr/sbin/useradd  # Location of useradd

if [ -f $FILE ]; then # Make sure the file exists
  # White space and/or tab separated... Best not to use this because
  # if you have a city name with a space (ie Las Vegas), then it will
  # not be correct
  #while read user group first after dob city
  while IFS=, read user agroup first after dob city # Comma separated
  do
    if [ "$user" != "" ]; then # Make sure we have a username (may want to enhance this test)
      echo "$user $agroup $first $after $dob $city"
      #echo
      #$USERADD -s $SHELL -m -G $agroup -c "$first,$after,$dob,$city" -p $user $user
      if [ $? -gt 0 ]; then
        echo "Error adding user: $user"
      else
        echo "Added user: $user"
      fi
    fi
  done <$FILE
else
  echo "No such file: $FILE"
fi
test.txt:
Code:
john,sys,John,Smith,01-01-2005,Chicago
bob,sys,Bob,Jones,01/01/2004,Las Vegas
It's just meant to show you one way of doing it, it doesn't do much in the way of sanity checks. But if you are absolutely certain of the integrity of the file, and it's format, that you are parsing, then it will work. The file format is "assumed" to be exactly as you said it would be, ie comma separated with every line in it being valid input. It does check for blank lines, but nothing beyond that. And as noted in the script, you shouldn't use tab delimited lines since it's possible for city names to have spaces and to the script a space is the same as a tab. It also assumes the additional group you are adding them to already exists. If it doesn't then see "man groupadd" and add code for that. Anyway, you may want to take a look at a shell scripting guide and add some more sanity checks and etc. Here's a pretty good one:
http://www.tldp.org/LDP/abs/html/

Last edited by DaHammer; 04-08-2005 at 02:38 AM.
 
Old 04-08-2005, 02:41 AM   #6
muppski
Member
 
Registered: Feb 2003
Posts: 149

Original Poster
Rep: Reputation: 15
O_O!!

WoW thanks
IŽll go from there

Right now I been playing with cut but I just found out cut dosent do the exact thing I was trying to output it with

IŽll post my final results here later
Thanks everyone
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding users Abbaddon Linux - Newbie 7 09-01-2004 04:30 AM
adding users... Digital_Err0r Linux - General 1 03-31-2004 03:08 PM
adding users Lucasite Linux - Newbie 3 03-04-2004 11:30 AM
Adding new users Fried Linux - Newbie 4 09-24-2001 10:37 AM
Adding users... philfighter Linux - General 3 07-23-2001 10:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration