<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Arne Aase &#187; Shell script</title>
	<atom:link href="http://www.arneaase.net/category/shell-script/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.arneaase.net</link>
	<description>Stuff i find interesting</description>
	<lastBuildDate>Tue, 17 Mar 2009 22:51:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Guide: Simple shell script &#8211; Adding users to linux from file</title>
		<link>http://www.arneaase.net/2008/09/guide-simple-shell-script-adding-users-to-linux-from-file/</link>
		<comments>http://www.arneaase.net/2008/09/guide-simple-shell-script-adding-users-to-linux-from-file/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 18:43:03 +0000</pubDate>
		<dc:creator>Arne Aase</dc:creator>
				<category><![CDATA[Shell script]]></category>
		<category><![CDATA[is209]]></category>

		<guid isPermaLink="false">http://www.arneaase.net/?p=13</guid>
		<description><![CDATA[So we&#8217;re going to add multiple users and it&#8217;s boring to write useradd -p &#60;password&#62; -c &#60;name&#62; &#60;username&#62; on every user we want to add. why don&#8217;t we try to make a script that add users listed in a file?
Lets take a look on how it&#8217;s possible to add users from a file with a [...]]]></description>
			<content:encoded><![CDATA[<p>So we&#8217;re going to add multiple users and it&#8217;s boring to write useradd -p &lt;password&gt; -c &lt;name&gt; &lt;username&gt; on every user we want to add. why don&#8217;t we try to make a script that add users listed in a file?</p>
<p>Lets take a look on how it&#8217;s possible to add users from a file with a little shell script.</p>
<p>First we make the file with users, in the file we type username password and name separated with , and newlines</p>
<p>ex:</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">Username1,pass1,full name1
Username2,pass2,full name2
username3,pass3,full name3
username4,pass4,full name4
username5,pass5,full name5</pre></div></div>

<p>Save the information in a file called users.txt, then create a new file called userscript.sh and open it in your editor.</p>
<p>Since it&#8217;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:</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">#!/bin/sh</pre></div></div>

<p>Then we&#8217;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.</p>
<p>The first function we write is the add function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">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 &amp;lt; $1
	exit 1
}</pre></td></tr></table></div>

<p>Okay, so what does this function actually do ? (described by line numbers)</p>
<ol>
<li>Define the function</li>
<li>Starts the function</li>
<li>Starts a loop which will loop through the file given as the first parameter line by line</li>
<li>Do the following on every line in the file</li>
<li>Create a variable named username, the variable should contain the info from line start to the first &#8220;,&#8221;</li>
<li>Create a variable named password, the variable should contain info from the first &#8220;,&#8221; to the second &#8220;,&#8221; and encrypt it to md5</li>
<li>Create a variable named name, the variable should contain info from the second &#8220;,&#8221; to the third &#8220;,&#8221;.</li>
<li>Force the /usr/sbin/useradd command to be run with root privileges</li>
<li>End while loop when every line given as the first parameter is processed</li>
<li>Exit the shell script with no errors</li>
<li>End function</li>
</ol>
<p>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).</p>
<p>Then we need to start with the delete function</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">del()
{
	while read line
	do
		username=`echo $line | cut -d',' -f1`
		sudo /usr/sbin/userdel $username
	done &amp;lt; $1
	exit 1
}</pre></div></div>

<p>I&#8217;m not going to explain this function as thorough as i did with the add function. The only difference is that we don&#8217;t need to get the password or name in order to delete, then we run the userdel command instead of the useradd command.</p>
<p>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.</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">help()
{
	echo &quot;usage: -a -d -h &lt;file&gt;&quot;
	echo &quot;    -a add users listed in file&quot;
	echo &quot;    -d delete users listed in file&quot;
	echo &quot;    -h print help&quot;
	exit 1
}</pre></div></div>

<p>finally we only have to write some code to get the users options and process the right function.</p>

<div class="wp_syntax"><div class="code"><pre class="language" style="font-family:monospace;">while getopts adh opt
do
	case ${opt} in
		a ) add ${!#};;
		d ) del ${!#};;
		h ) help;;
		\? ) help;;
	esac
done
&nbsp;
help</pre></div></div>

<p>This code loops through the arguments the user gave and run the appropriate function, if the user specify a option which doesn&#8217;t exist the help menu will be displayed, this also happens if the user doesn&#8217;t type a option at all.</p>
<p>You can use this script by opening a terminal and navigate to the correct folder and write &#8220;sh userscript.sh -a users.txt&#8221;, note that you have do be logged in as a user who have sudo privileges or as root in order to create the users.<br />
It would be smart to add a echo command saying which or how many users that where added/deleted.</p>
<p>Full source can be viewed/downloaded here:<br />
<a href="http://www.arneaase.net/resources/shell/users.txt">Users.txt</a><br />
<a href="http://www.arneaase.net/resources/shell/userscript.sh">Userscript.sh</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.arneaase.net/2008/09/guide-simple-shell-script-adding-users-to-linux-from-file/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
