So we’re going to add multiple users and it’s boring to write useradd -p <password> -c <name> <username> on every user we want to add. why don’t we try to make a script that add users listed in a file?
Lets take a look on how it’s possible to add users from a file with a little shell script.
First we make the file with users, in the file we type username password and name separated with , and newlines
ex:
Username1,pass1,full name1 Username2,pass2,full name2 username3,pass3,full name3 username4,pass4,full name4 username5,pass5,full name5
Save the information in a file called users.txt, then create a new file called userscript.sh and open it in your editor.
Since it’s a shell script we need to tell the interpreter that it should interpret this file as a /bin/sh script, this can be done by writing:
#!/bin/sh
Then we’re all set to start writing the script, first of we need to think of what the script should do. Should it be possible to delete users from file ? In my example I will give the administrator a choice to add or delete users from file. Since we dont want to make 2 separate files with useradd script and userdel script we need to make functions and process correct function by options.
The first function we write is the add function:
1 2 3 4 5 6 7 8 9 10 11 | add()
{
while read line
do
username=`echo $line | cut -d',' -f1`
password=`echo $line | cut -d',' -f2 | mkpasswd -H md5 -s`
name=`echo $line | cut -d',' -f3`
sudo /usr/sbin/useradd -c '$name' -p $password $username
done < $1
exit 1
} |
Okay, so what does this function actually do ? (described by line numbers)
- Define the function
- Starts the function
- Starts a loop which will loop through the file given as the first parameter line by line
- Do the following on every line in the file
- Create a variable named username, the variable should contain the info from line start to the first “,”
- Create a variable named password, the variable should contain info from the first “,” to the second “,” and encrypt it to md5
- Create a variable named name, the variable should contain info from the second “,” to the third “,”.
- Force the /usr/sbin/useradd command to be run with root privileges
- End while loop when every line given as the first parameter is processed
- Exit the shell script with no errors
- End function
As you can see we use different shell commands in order to extract and process the information we want. The cut command cuts a string on a given delimiter (-d option), and -f says which cut i want (f1 first cut, f2 second cut etc).
Then we need to start with the delete function
del()
{
while read line
do
username=`echo $line | cut -d',' -f1`
sudo /usr/sbin/userdel $username
done < $1
exit 1
}I’m not going to explain this function as thorough as i did with the add function. The only difference is that we don’t need to get the password or name in order to delete, then we run the userdel command instead of the useradd command.
We also need to write a little help function in order to show the user how to use this little script, this is a simple functions which echo how to use the program.
help()
{
echo "usage: -a -d -h <file>"
echo " -a add users listed in file"
echo " -d delete users listed in file"
echo " -h print help"
exit 1
}finally we only have to write some code to get the users options and process the right function.
while getopts adh opt
do
case ${opt} in
a ) add ${!#};;
d ) del ${!#};;
h ) help;;
\? ) help;;
esac
done
helpThis code loops through the arguments the user gave and run the appropriate function, if the user specify a option which doesn’t exist the help menu will be displayed, this also happens if the user doesn’t type a option at all.
You can use this script by opening a terminal and navigate to the correct folder and write “sh userscript.sh -a users.txt”, note that you have do be logged in as a user who have sudo privileges or as root in order to create the users.
It would be smart to add a echo command saying which or how many users that where added/deleted.
Full source can be viewed/downloaded here:
Users.txt
Userscript.sh

Hubert says:
A great secret of success is to go through life as a man who never gets used up.
November 23, 2009, 12:23facebook chips says:
although I consume almost all of my daytime hours on the internet enjoying online games like facebook poker or mafia wars, I always like to take some spare time to surf a number of websites on occasion and I’m proud to report this recent page is in actual fact quite good quality and substantially more beneficial than 1 / 2 the other worthless junk I read today , at any rate i’m off to enjoy a smattering of hands of zynga poker
March 20, 2010, 04:42Ebook Guidebook says:
Fantastic article. There’s a lot of good information here, though I did want to let you know something – I am running Ubuntu with the up-to-date beta of Internet Explorer, and the look and feel of your site is kind of rakish for me. I can understand the articles, but the navigation doesn’t function so solid.
March 31, 2010, 10:57Gold Price Boy says:
I seriously can’t believe it. I pray Wayne Rooney is healed for the cup!
March 31, 2010, 17:44Andreas Burdett says:
Extremely interesting post thanks for sharing I have added your site to my favorites and will check back
By the way this is off topic but I really like your web page layout.
May 14, 2010, 07:21dulles airport car rental says:
Excellent publish. Bookmarked it already. have a nice day, mate.
May 14, 2010, 13:41j 41 shoes says:
Great post! I’d like to see something that’s pretty simple and easy to understand at a glance, but with a lot of motion and activity to reflect the vibrancy of the community. Can’t wait to see what you and morgamic come up with.
June 30, 2010, 10:44