Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-03-2003, 10:27 PM   #1
LQ Newbie
Registered: Sep 2003
Location: new zealand
Posts: 8

Rep: Reputation: 0
Question bash script for adding multiple users

hi all,

the first post wasnt complete so i edited it now.
I have been given a task to write a script that will add a group of users from an input file. This shell script has to also provide some help information when users use a wrong command pattern.

•If user start myaddusers incorrectly, i.e. without any parameters or too many parameters, the program should show the basic help for the user. For example, show the usage of command

•If user type myaddusers –h, the program will give detailed information about the program and userlistfile structure.
The userlist file is a text file, which contains the users’ details to create the accounts. The userlist file has the following structure.
• Each line is a user record.
• The record has 4 fields

I have no idea where to start etc

does anyone know where i can find simular script so i can make some changes to it ?

thasnks a lot


Last edited by pilipk01; 09-04-2003 at 12:40 AM.
Old 09-04-2003, 03:34 PM   #2
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 67
Welcome to LQ.

You probably want to start with somehitng like:

if [ ! $1 ] || [ "$1" == "-h" ]; then
echo You did it wrong!!!
elif [ -f $1 ];then
  for line in `cat $1`;do
  USER=`echo $line | cut -d":" -f1`
  PASS=`echo $line | cut -d":" -f2`
  GRUP=`echo $line | cut -d":" -f3`
  FULL=`echo $line | cut -d":" -f4`

  # Do your stuf with the info from the variables as you wish
  echo Added $USER $FULL

Old 09-04-2003, 04:43 PM   #3
Registered: Apr 2002
Location: Los Gatos, CA
Distribution: boring redhat 9
Posts: 163

Rep: Reputation: 30
I knew how to do this easily in PERL, but the challenge of doing it in bash was sweet, considering my bash skills are somewhat lacking. This should get you quite on your way to exactly what your need. Right now it will do everything your post required.

(it's kinda long, so I linked it)
The script itself

Some things to reference:

Man page for groupadd
Man page for useradd
Man page for chfn (change full name)
Old 01-12-2004, 12:40 AM   #4
Registered: Jan 2004
Location: Austin, TEXAS
Distribution: CentOS 6.5
Posts: 211

Rep: Reputation: 32
perl script doesn't work because of groups

That Perl script is pretty well-designed, but it doesn't work on Mandrake 9.2 and probably a lot of other distros. It assumes group names are in the file. Mandrake keeps groups in /etc/groups and uses and index number to reference them in /etc/passwd. Trying to add the groups using the -g also fails.

I'm too clumsy in Perl to improve on this script, but if someone else could modify it to utilize guids, that would be really damn cool.


Old 01-12-2004, 11:05 PM   #5
Registered: Apr 2002
Location: Los Gatos, CA
Distribution: boring redhat 9
Posts: 163

Rep: Reputation: 30
I'll take a gander at it in the morning. Should be just a matter of the user adding a switch (--guid) to tell it that the group listed in the INPUT file is already in numberic form, instead of named form.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 new users via Shell script coolfrog Linux - General 10 12-05-2010 11:47 AM
adding a rotating - to my bash script Cinematography Programming 3 08-31-2005 02:05 PM
Adding multiple user shell script plexus Programming 2 06-19-2004 09:36 PM
Need help adding an IF / ELSE statement to my Bash Script Relix Linux - General 1 08-01-2002 03:03 PM
Script that would automate adding users zyft02 Linux - Newbie 4 02-26-2002 12:19 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:40 PM.

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