- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Script fo creating multiple users
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-17-2009 03:02 AM
тАО04-17-2009 03:02 AM
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)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-17-2009 03:08 AM
тАО04-17-2009 03:08 AM
Re: Script fo creating multiple users
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-17-2009 03:08 AM
тАО04-17-2009 03:08 AM
Re: Script fo creating multiple users
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-17-2009 03:48 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-17-2009 04:07 AM
тАО04-17-2009 04:07 AM
Re: Script fo creating multiple users
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`
Ganesh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-18-2009 06:31 AM
тАО04-18-2009 06:31 AM
Re: Script fo creating multiple users
##### 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2009 08:12 PM
тАО04-20-2009 08:12 PM
Re: Script fo creating multiple users
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-20-2009 08:13 PM
тАО04-20-2009 08:13 PM
Re: Script fo creating multiple users
Regards,
C.Sacthivel