<?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; is209</title>
	<atom:link href="http://www.arneaase.net/category/is209/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>
		<item>
		<title>Getting started with vim</title>
		<link>http://www.arneaase.net/2008/09/getting-started-with-vim/</link>
		<comments>http://www.arneaase.net/2008/09/getting-started-with-vim/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 14:27:08 +0000</pubDate>
		<dc:creator>Arne Aase</dc:creator>
				<category><![CDATA[Editor]]></category>
		<category><![CDATA[is209]]></category>

		<guid isPermaLink="false">http://www.arneaase.net/?p=12</guid>
		<description><![CDATA[Earlier this semester our teacher gave us an assignment to learn vi (vim or gvim), and make a sheet cheat in order to learn how powerful the vim editor is. When I tried to make a sheet cheat I didn&#8217;t feel that I was learning something, which is the reason I started to look for [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier this semester our teacher gave us an assignment to learn vi (vim or gvim), and make a sheet cheat in order to learn how powerful the vim editor is. When I tried to make a sheet cheat I didn&#8217;t feel that I was learning something, which is the reason I started to look for other ways to learn how to use the editor. I stumbled upon a tutorial which is installed automatic if you install the full version of vim (most linux distributions come with a light version of vim so you have to install full version in order to get the tutorial).</p>
<p>To start the tutorial you just open a terminal and type &#8220;vimtutor&#8221;, the tutorial encourage the learning by doing method and is divided into small chapters which give you more and more insight on how powerful the editor is. The tutorial is opened in vim and it encourage you to do small tasks inside the tutorial while you&#8217;re reading.</p>
<p>I found this tutorial very efficient on learning the basic stuff, and would recommend every new user to go through this guide a couple of times instead of writing a sheet cheat. After you feel comfortable with the basics and how commands work it&#8217;s possible to make a sheet cheat with more advanced commands which isn&#8217;t used that often.</p>
<p>If you&#8217;re going to write code in the vim editor I would suggest to enable some features which gives you syntax highlight and smart indentation etc (type :help in normal mode to get more information on what you can do). If you&#8217;re going to write documents i would suggest to take a look at <a href="http://www.latex-project.org/intro.html">laTeX</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arneaase.net/2008/09/getting-started-with-vim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Free software, Open Source and Proprietary Software ?</title>
		<link>http://www.arneaase.net/2008/08/what-is-free-software-open-source-and-proprietary-software/</link>
		<comments>http://www.arneaase.net/2008/08/what-is-free-software-open-source-and-proprietary-software/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 00:19:30 +0000</pubDate>
		<dc:creator>Arne Aase</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[is209]]></category>

		<guid isPermaLink="false">http://www.arneaase.net/?p=10</guid>
		<description><![CDATA[Free Software, Open Source and Proprietary Software is different views on which right the users should have on the software they use (modify, study, redistribute or sell the software).
Free Software Foundation is all about freedom to the people and letting the people choose to do whatever they want with the software (Modify, study, redistribute or [...]]]></description>
			<content:encoded><![CDATA[<p>Free Software, Open Source and Proprietary Software is different views on which right the users should have on the software they use (modify, study, redistribute or sell the software).</p>
<p><a href="http://www.fsf.org/">Free Software Foundation</a> is all about freedom to the people and letting the people choose to do whatever they want with the software (Modify, study, redistribute or sell), but the licence must follow the software.</p>
<p><a href="http://www.opensource.org/">Open Source Initiative</a> is for freedom to the people and are more business friendly than Free Software since they make it possible to add some restrictions, but they have a <a href="http://www.opensource.org/docs/osd">list of criteria</a> the licence must follow in order to be open source software.</p>
<p><a href="http://">proprietary software</a> is more concerned with the producer and restrict users by having copyright, patents and closed source code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arneaase.net/2008/08/what-is-free-software-open-source-and-proprietary-software/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
