LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 08-20-2005, 06:21 AM   #1
amer_58
Member
 
Registered: Mar 2004
Distribution: Slackware 10.2
Posts: 213

Rep: Reputation: 30
add multiple users(Script)


Soon I will be asked to add multiple users on our Linux server, I will have a text file with a 30 names, I need a script that will add the 30 users+permission, can anyone help me or guide me to that, also what about the permission, can the permission be input from the text file, Thanks for your help.
 
Old 08-20-2005, 07:47 AM   #2
amer_58
Member
 
Registered: Mar 2004
Distribution: Slackware 10.2
Posts: 213

Original Poster
Rep: Reputation: 30
while searching i found this

http://www.linuxquestions.org/questi...hreadid=352419

but i couldnt really understand how does it work.
 
Old 08-20-2005, 02:39 PM   #3
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
What's the format of the names file? You'll need to know the name you want to use as the user's ID (the name they'll login with, and (not essential) their full name for the GECOS (comment) field. Your script can read the information from the name file with code like
Code:
cat namefile | while read user
do
    # do the useradd
done
man useradd to see what you have to specify to create the user entry. Use $user for the ID of the user you're adding.

As to permissions, you can use chmod to change that if you're not happy with the defaults - you can refer to the user's home directory as ~username so you don't need to know the actual home directory path.

Have a go and ask again if you get stuck.
 
Old 08-20-2005, 06:47 PM   #4
amer_58
Member
 
Registered: Mar 2004
Distribution: Slackware 10.2
Posts: 213

Original Poster
Rep: Reputation: 30
what am thinking of sth like this:

file allusres.txt

firstname lastname username
.
.
.
.
.
.
.20 times.
 
Old 08-20-2005, 08:49 PM   #5
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
how about when you've read the line,
Code:
echo $line | sed 's/ /\n/g' | { read first; read last; read user; };
... and then as in the outlined script.

hth --Jonas
 
Old 08-21-2005, 09:22 AM   #6
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
How about ...
Process hog! Better (IMHO)
Code:
cat namefile | while read first last user
do
    # do the useradd
    /usr/sbin/useradd -c "$first $last" -m $user
done
 
Old 08-21-2005, 09:34 AM   #7
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Much better :-)

You know, those manuals don't seem so silly after all .

--Jonas
 
Old 08-21-2005, 12:43 PM   #8
amer_58
Member
 
Registered: Mar 2004
Distribution: Slackware 10.2
Posts: 213

Original Poster
Rep: Reputation: 30
Quote:
cat namefile | while read first last user
do
# do the useradd
/usr/sbin/useradd -c "$first $last" -m $user
done
Thanks eddiebaby1023 but could you explain a bit more please,

how does the while works, the first last user how would be recognized?

sorry am just new into this?
 
Old 08-21-2005, 01:29 PM   #9
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
first, last and user are each seperated by a character in $IFS (`Internal Field Seperator' or similar).

try
Code:
$ IFS='x'
$ echo "aaxbbxcc" | read a b c
$ echo $b $c $a
hth --Jonas

btw, couldn't one also do "while ... done < namefile" instead of catting?
 
Old 08-21-2005, 03:17 PM   #10
amer_58
Member
 
Registered: Mar 2004
Distribution: Slackware 10.2
Posts: 213

Original Poster
Rep: Reputation: 30
ok this is what i did, new text file and saved as users.sh
Code:
 
#!/bin/sh 
cat names | while read first last user 
do 
    # do the useradd 
    /usr/sbin/useradd -c "$first $last" -m $user 
done
i run the scpript i did
cat /etc/passwd | cut -d ":" -f1
and the new user was added + $Home.

but i still have problem understanding this line:
Code:
 
while read first last user
last and user are just simple text how does the system knows that first would be the name of the user...etc!! am just trying to understand, and $first $last $user are dynamic var?, how they are connected with first last and user, what am trying to say how does the system knows the order of content of the file?

//edit

when i tried adding more users on my text it would only add the first line
test test1 test2
alex alex1 alex2
only test2 will be added!

Thanks.

Last edited by amer_58; 08-21-2005 at 03:35 PM.
 
Old 08-21-2005, 04:36 PM   #11
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Quote:
Code:
while read first last user
`first', `last' and `user' are just names of variables. We might as well have called them foo, bar and baz, but that obscures their meaning.

The system `knows' that it's the first-/last-/username of the new user, because useradd expects them to be in particular places and the script puts them the right places (see man useradd).

hth --Jonas
 
Old 08-21-2005, 04:48 PM   #12
amer_58
Member
 
Registered: Mar 2004
Distribution: Slackware 10.2
Posts: 213

Original Poster
Rep: Reputation: 30
thanks jonaskoelker for your time, but why the loop does not go to the next line?
 
Old 08-21-2005, 05:01 PM   #13
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
One guess: does the file end in a newline character? It has to; otherwise read won't read the last item. Try a file with more than two items.

hth --Jonas
 
Old 08-21-2005, 05:23 PM   #14
amer_58
Member
 
Registered: Mar 2004
Distribution: Slackware 10.2
Posts: 213

Original Poster
Rep: Reputation: 30
ok lets say this is my file:
test test1 test2
alex alex2 alex3
win win1 win2

the system will only add the win2! not really sure why this happening!!
 
Old 08-21-2005, 06:19 PM   #15
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Weird--first it only adds the first user, then only the last user.

Might it be that some of the users were already added?

Try replacing the loop body with echo.

hth --Jonas
 
  


Reply


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
Shell Script to add samba users dickohead Programming 11 03-28-2012 06:50 PM
Shell Script: Add Users from file flobadon Programming 3 12-07-2004 03:49 PM
Script to add users ssudhi Linux - Newbie 3 05-01-2004 04:33 AM
Script thats add users automatically embalmedlenin *BSD 1 09-14-2003 04:34 PM
script for creating multiple users d3funct Programming 1 08-21-2001 05:22 PM


All times are GMT -5. The time now is 05:13 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration