linux script for adding users

BriGy86

Diamond Member
Sep 10, 2004
4,537
1
91
so far i have 2 comments and "clear"

i have a file that has 5 users in it with info seperated by ":'s"

i know i have to use "useradd" and probably "InRec"

but i am completely f*cking clueless as to how to start this thing

any info or help would be nice
 

DaiShan

Diamond Member
Jul 5, 2001
9,617
1
0
from the man page of useradd '$man useradd'

NAME
useradd - Create a new user or update default new user information

SYNOPSIS
useradd [-c comment] [-d home_dir]
[-e expire_date] [-f inactive_days]
[-g initial_group] [-G group[,...]]
[[-M] -m [-k skeleton_dir]] [-o] [-p passwd]
[-s shell] [-u uid] login

/edit I'm partial to superadduser, it is an interactive method to add users (asks you the username the real name etc)
 

BriGy86

Diamond Member
Sep 10, 2004
4,537
1
91
Originally posted by: DaiShan
from the man page of useradd '$man useradd'

NAME
useradd - Create a new user or update default new user information

SYNOPSIS
useradd [-c comment] [-d home_dir]
[-e expire_date] [-f inactive_days]
[-g initial_group] [-G group[,...]]
[[-M] -m [-k skeleton_dir]] [-o] [-p passwd]
[-s shell] [-u uid] login

/edit I'm partial to superadduser, it is an interactive method to add users (asks you the username the real name etc)

but i need this script to read a simple text file that has seperated fields and i need it to pick out a field and make a user name out of it, 5 times, all automaticly when the script is run

thats what i am confused about

im guessing i start this script with "read" or something like that, but other than that and using "useradd" im lost

(thank you for the response)
 

lopingho

Senior member
Dec 27, 1999
809
0
76
I have a feeling that this could be done pretty easily with perl. I think you would need to open a filehandle and then create a sub that runs useradd.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
A little bash

for x in user1 user2 user3 user4 user5 ; do useradd $x ; done

or a little faster if its in a file 1 user name per line

cat userlist.txt |while read line; do useradd "${line}"; done


of course you will want to add more options to useradd. its simple, but bash is your friend. For more complex things you might use awk to split up the line.
 

drag

Elite Member
Jul 4, 2002
8,708
0
0
Originally posted by: BriGy86
Originally posted by: DaiShan
from the man page of useradd '$man useradd'

NAME
useradd - Create a new user or update default new user information

SYNOPSIS
useradd [-c comment] [-d home_dir]
[-e expire_date] [-f inactive_days]
[-g initial_group] [-G group[,...]]
[[-M] -m [-k skeleton_dir]] [-o] [-p passwd]
[-s shell] [-u uid] login

/edit I'm partial to superadduser, it is an interactive method to add users (asks you the username the real name etc)

but i need this script to read a simple text file that has seperated fields and i need it to pick out a field and make a user name out of it, 5 times, all automaticly when the script is run

thats what i am confused about

im guessing i start this script with "read" or something like that, but other than that and using "useradd" im lost

(thank you for the response)

It'll probably be pretty simple. If you can give a sample of the text fileit would be easier to whip up a script that would actually work. If you give a few lines, even if the information has been changed to make it safe to post on the internet you'd probably have a few good suggestions in a short while.

Check out my sig the the linux document project's guides. They have a introduction to bash and then a advanced bash scripting guide.

In a Linux shell you'll have items like grep, awk, sed, and such for manipulating text. You can do text mangling with variables and such to do stuff like combining a first and the 3 first letters of the last name to generate a username that would be more unique.

Then there are the whole regular expressions for seperating out feilds of text and changing characters and whatnot. Although I think that with regular expressions it may be worth your while to take a look at perl.

Perl can be used in a command line or bash script in a similar manner to awk, grep, sed, but it can replace all those things. Similar syntaxes also since they all share a common background.


A simple way to filter out some stuff with awk...

Say you have a file with...
1:2:3:4:5
A:B:C:E
a:b:c:e
!:#:$:%:^

Say that is named "litter" and you want the 3rd feild and the 1st feild printed in that order.

So make the first line blank (I don't know why awk has this problem with the first line of input)

and go
cat litter | awk '{ FS=":" } { print $3" "$1 }'


Of course for something like this you'd want to make a actual script you can save and do testing to make sure that you have absolutely the correct output before making it so that it would run through a adduser or useradd script. All lower case letters and stuff like this.

If you want something very complex perl is good at text manipulations.
 

drag

Elite Member
Jul 4, 2002
8,708
0
0
Originally posted by: n0cmonkey
Your distro doesn't have adduser?

Ya.. that can be a bit confusing.

adduser
useradd

a couple different commands most distros have...
 

BriGy86

Diamond Member
Sep 10, 2004
4,537
1
91
Originally posted by: sourceninja
A little bash

for x in user1 user2 user3 user4 user5 ; do useradd $x ; done

or a little faster if its in a file 1 user name per line

cat userlist.txt |while read line; do useradd "${line}"; done


of course you will want to add more options to useradd. its simple, but bash is your friend. For more complex things you might use awk to split up the line.

i believe something like this will help a lot

the file has 5 rows

example:
phone number:first name:last name:middle int.:4 digit number:job title: Date started

there are 5 of these lines

i think i need to specify the field to read for the user name though after the "while read line" right? like field 2 or 3

and then i just add the line number in place of "${line}" right?

and i do this 5 times?

[and thank you for all the responses, you have no idea how frustrated i have been with this]
 

n0cmonkey

Elite Member
Jun 10, 2001
42,936
1
0
Something like this might work. I don't know how to run system commands directly from perl (I'm too lazy to look it up at the moment ), but this gives you a script to run instead. Yes, it's ugly and stupid.

EDIT: Small fix, I think

 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
I belive you would use exec() or system() to call programs from perl. Its been a long time.

A quick google and I found this

@args = ("command", "arg1", "arg2");
system(@args) == 0
or die "system @args failed: $?"
 

nweaver

Diamond Member
Jan 21, 2001
6,813
1
0
Originally posted by: n0cmonkey
Something like this might work. I don't know how to run system commands directly from perl (I'm too lazy to look it up at the moment ), but this gives you a script to run instead. Yes, it's ugly and stupid.

Perl will do that to you...makes you very very lazy.

'Nix does it to you too...the lazy man's OS
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |