LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script for adding multiple users (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-for-adding-multiple-users-89022/)

pilipk01 09-03-2003 09:27 PM

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
Username
Password
Groupname
Fullname

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

chris
:confused:

david_ross 09-04-2003 02:34 PM

Welcome to LQ.

You probably want to start with somehitng like:
Code:

#!/bin/bash

if [ ! $1 ] || [ "$1" == "-h" ]; then
echo You did it wrong!!!
elif [ -f $1 ];then
IFS="
"
  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

  done
fi


Saraev 09-04-2003 03:43 PM

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)

di11rod 01-11-2004 11:40 PM

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.

thanks,

di11rod

Saraev 01-12-2004 10:05 PM

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.


All times are GMT -5. The time now is 01:50 PM.