Operating System - Linux
1755847 Members
4999 Online
108838 Solutions
New Discussion юеВ

Re: Script fo creating multiple users

 
SOLVED
Go to solution
sacthivel.C
Frequent Advisor

Script fo creating multiple users

Dear all,
I am new to scripting.
My requirement is to create a script for creating multiple users in linux.
So i planned to append all the user login names in one file and call that file through the script.
But it is not possible for me to increment the UID.I attached here the script which i made.
Would some body help me to solve it?

Regards,
C.Sacthivel.

(pls open the attachment in wordpad)
7 REPLIES 7
sacthivel.C
Frequent Advisor

Re: Script fo creating multiple users

#!/bin/sh

lastuid=$(cat /etc/passwd | cut -d: -f3 | tail -1)
for afile in $(cat /root/usrlist)

do

lastuid++
useradd -u $lastuid -g 21 -d /home/$afile -s /bin/bash -p $afile $afile


done

This is the script.

Regards,
C.Sacthivel
sacthivel.C
Frequent Advisor

Re: Script fo creating multiple users

#!/bin/bash

lastuid=$(cat /etc/passwd | cut -d: -f3 | tail -1)
for afile in $(cat /root/usrlist)

do

lastuid++
useradd -u $lastuid -g 21 -d /home/$afile -s /bin/bash -p $afile $afile


done

This is the script.

Regards,
C.Sacthivel
Ganesan R
Honored Contributor
Solution

Re: Script fo creating multiple users

Hi Sacthivel,

You don't need to specify the UID argument for useradd command.

by default useradd command will take the next unused UID for the new user if you don't specifed.
Best wishes,

Ganesh.
Ganesan R
Honored Contributor

Re: Script fo creating multiple users

Hi Sacthivel,

One more thing I want to ask you..

lastuid=$(cat /etc/passwd | cut -d: -f3 | tail -1)

This variable take the last entry UID value from /etc/passwd. But how can be make sure that this value is lowest UID in /etc/passwd file. It is always possible that higher UID's exist in passwd file.

Other thing is, lastuid++ won't work in shell script. Instead you can manually increament the value inside the loop like this

lastuid=`expr $lastuid + 1`

Best wishes,

Ganesh.
Viktor Balogh
Honored Contributor

Re: Script fo creating multiple users

try something like this:

##### START OF CODE #####

#!/bin/sh

while read username group gecos
do
grep -q "^${group}:" /etc/group
if [ $? -eq 0 ]; then
useradd -g ${group} -m -d /home/${username} -c ${gecos} -s /bin/bash ${user}
else
echo "No such group: ${group}"
echo "User skipped"
fi
done < userlist

##### END OF CODE #####

in the 'userlist' file you should include all the users to add, in the following format: (one line per user)

- first field is the users login name
- second field is the primary group
- the other fields starting from 3 will be the GECOS (comment) field in the /etc/passwd, containing the users real name, etc...

Sample:

user1 group1 Joe Hedgehog
tim groupoftim Tim the bartender
jim defaultgroup Jim Bo

PS:I'm not sure if the group existence should be checked or the useradd command will skip by itself. Currently I don't have access to a linux machine.
****
Unix operates with beer.
sacthivel.C
Frequent Advisor

Re: Script fo creating multiple users

Dear All,
Thanks for every body for answering my forum and particularly to Mr Ganesan.
My requirement is that i have to create users with same group id and same shell.

So i put the below script.

####################################

#!/bin/bash
cat /etc/passwd | cut -d: -f1 > exi.users
grep -vf exi.users new.users > usrlist

for afile in $(cat /root/users/usrlist)
do

useradd -g 21 -d /home/$afile -s /bin/bash $afile
passwd $afile

done

###################################

1.first of all i am getting the existing users list and append it to the file named "exi.users"
2.I asked the users to add their login names to the file named "new.users"
3.After that i am comparing both the files and append it's output to a file called "usrlist".

Then the remaining procedure is same.

Once again thank you for both of them.

Regards,
C.Sacthivel
sacthivel.C
Frequent Advisor

Re: Script fo creating multiple users

Thanks a lot once again.

Regards,
C.Sacthivel