LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   add multiple users(Script) (https://www.linuxquestions.org/questions/programming-9/add-multiple-users-script-355136/)

amer_58 08-20-2005 06:21 AM

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.

amer_58 08-20-2005 07:47 AM

while searching i found this

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

but i couldnt really understand how does it work.

eddiebaby1023 08-20-2005 02:39 PM

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.

amer_58 08-20-2005 06:47 PM

what am thinking of sth like this:

file allusres.txt

firstname lastname username
.
.
.
.
.
.
.20 times.

jonaskoelker 08-20-2005 08:49 PM

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

eddiebaby1023 08-21-2005 09:22 AM

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


jonaskoelker 08-21-2005 09:34 AM

Much better :-)

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

--Jonas

amer_58 08-21-2005 12:43 PM

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?

jonaskoelker 08-21-2005 01:29 PM

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?

amer_58 08-21-2005 03:17 PM

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.

jonaskoelker 08-21-2005 04:36 PM

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

amer_58 08-21-2005 04:48 PM

thanks jonaskoelker for your time, but why the loop does not go to the next line?

jonaskoelker 08-21-2005 05:01 PM

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

amer_58 08-21-2005 05:23 PM

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!!

jonaskoelker 08-21-2005 06:19 PM

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


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