<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Bash In Action</title>
	<atom:link href="http://bashinaction.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://bashinaction.wordpress.com</link>
	<description>Shell scripting experiments of a beginner</description>
	<lastBuildDate>Wed, 06 Jan 2010 16:10:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='bashinaction.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/d3e991dca6a22800a5695fbf02e6875e?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Bash In Action</title>
		<link>http://bashinaction.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://bashinaction.wordpress.com/osd.xml" title="Bash In Action" />
	<atom:link rel='hub' href='http://bashinaction.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Who will win the election? &#8211; Part 2 &#8211; AWK</title>
		<link>http://bashinaction.wordpress.com/2010/01/06/who-will-win-the-election-part-2-awk/</link>
		<comments>http://bashinaction.wordpress.com/2010/01/06/who-will-win-the-election-part-2-awk/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 16:10:41 +0000</pubDate>
		<dc:creator>thameera</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://bashinaction.wordpress.com/?p=35</guid>
		<description><![CDATA[I was surfing the web today here and there when suddenly I stumbled upon a page in The Geek Stuff which explained a shell command called awk. It&#8217;s a great tool to search through a text file for a pattern. The site contained many easy to understand examples on the usage of awk. The basic [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bashinaction.wordpress.com&amp;blog=11064688&amp;post=35&amp;subd=bashinaction&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was surfing the web today here and there when suddenly I stumbled upon a <a href="http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples">page</a> in The Geek Stuff which explained a shell command called <strong>awk</strong>. It&#8217;s a great tool to search through a text file for a pattern. The site contained many easy to understand examples on the usage of awk.<br />
The basic syntax is:</p>
<pre>awk '/search pattern 1/ {actions1}
   /search pattern 2/ {actions2}' filename</pre>
<p>Here, <em>actions1</em> is activated when the search pattern 1 is found and <em>actions2</em> when search pattern 2 is found. The data is taken from <em>filename</em>.<br />
This command can be manipulated in various ways to achieve many complex tasks in easily. The thought that instantly came to my mind was the <em>Who will win the election</em> script I wrote in the <a href="http://bashinaction.wordpress.com/2009/12/31/who-will-win-the-election/">previous blog post</a>. In that, the scripts creates a file named <em>results</em> which will store the results of the poll. An example of a <em>results</em> file is as follows:</p>
<pre>MR tham
MR sand
SF hugo
MR bosh
SF dialz</pre>
<p>Here, three people (tham, sand and bosh) think that MR will win the election and the other two think that SF will win. Okay, that script we wrote the last time creates this file, but how can we get a summary of the results after the poll? We want to know how many voted for MR and SF respectively.<br />
AWK comes to the rescue. Here&#8217;s a script to count the number of votes for MR and SF and display them:</p>
<pre>#!/bin/bash
#: Description: Gives a summary of the results of Who Will Win
     the Election?

awk 'BEGIN { mr=0;sf=0; }
$1 ~ /MR/ { mr++; }
/SF/ { sf++; }
END {print "MR =",mr,"\nSF =",sf;}' results</pre>
<p>It&#8217;s just one command! The output for the <em>results</em> file given above will be as follows:</p>
<pre>MR = 3
SF = 2</pre>
<p>Seems awk is a great tool to manipulate text files. For more info on awk visit <a href="http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples">this page</a>.<br />
So bye for now! See ya next time with another script!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bashinaction.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bashinaction.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bashinaction.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bashinaction.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bashinaction.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bashinaction.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bashinaction.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bashinaction.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bashinaction.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bashinaction.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bashinaction.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bashinaction.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bashinaction.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bashinaction.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bashinaction.wordpress.com&amp;blog=11064688&amp;post=35&amp;subd=bashinaction&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bashinaction.wordpress.com/2010/01/06/who-will-win-the-election-part-2-awk/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/86ce735f59af85b35fa7416c4b330452?s=96&#38;d=monsterid&#38;r=G" medium="image">
			<media:title type="html">thameera</media:title>
		</media:content>
	</item>
		<item>
		<title>Who will win the election?</title>
		<link>http://bashinaction.wordpress.com/2009/12/31/who-will-win-the-election/</link>
		<comments>http://bashinaction.wordpress.com/2009/12/31/who-will-win-the-election/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 05:02:33 +0000</pubDate>
		<dc:creator>thameera</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://bashinaction.wordpress.com/?p=27</guid>
		<description><![CDATA[Okay, it&#8217;s the election time here in Sri Lanka. The presidential election will be held on 26th January 2010. As the heat continue to build, I thought of writing a small script to get to know who will win the election as people think. To make it more user friendly, had to add some GUI [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bashinaction.wordpress.com&amp;blog=11064688&amp;post=27&amp;subd=bashinaction&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Okay, it&#8217;s the election time here in Sri Lanka. The presidential election will be held on 26th January 2010. As the heat continue to build, I thought of writing a small script to get to know who will win the election as people think. To make it more user friendly, had to add some GUI features. Hadn&#8217;t known how to use these message boxes, etc before, but a simple search in google provided the answers. Here&#8217;s the script:</p>
<pre>#!/bin/bash

name=$(zenity --entry --text "Please enter your name"
   --entry-text "" --title "Presidential Election 2010")
if [ $? != 0 ]
then
exit 0
fi

vote=$(gdialog --title "Presidential Election 2010" --radiolist
   "Who will win?" 60 100 10 1 Mahinda Rajapaksha 2 Sarath
   Fonseka 2&gt;&amp;1)

if [ $vote = "1" ]; then
result="MR"
fi
if [ $vote = "2" ]; then
result="SF"
fi

result="$result $name"
echo $result &gt;&gt; results</pre>
<p>The script will first prompt the user to enter his name. If he didn&#8217;t press <em>Cancel</em> in that name entry box, it will show the user a box with two radio buttons with the names of the two candidates, Mahinda Rajapaksha and Sarath Fonseka. Once the user selects an answer and presses <em>Ok</em> that vote will be appended to a text file <em>results</em> along with his name.<br />
The <em>Zenity</em> thing in the script is way to display dialog boxes and such GUI accessories in the shell scripts. Hope to add more of this later.<br />
<br />
As far as the votes in our boarding place are concerned, more than 90% percent think Mahinda will win the election. Let&#8217;s place that choice in the voters&#8217; hands. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bashinaction.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bashinaction.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bashinaction.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bashinaction.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bashinaction.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bashinaction.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bashinaction.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bashinaction.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bashinaction.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bashinaction.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bashinaction.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bashinaction.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bashinaction.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bashinaction.wordpress.com/27/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bashinaction.wordpress.com&amp;blog=11064688&amp;post=27&amp;subd=bashinaction&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bashinaction.wordpress.com/2009/12/31/who-will-win-the-election/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/86ce735f59af85b35fa7416c4b330452?s=96&#38;d=monsterid&#38;r=G" medium="image">
			<media:title type="html">thameera</media:title>
		</media:content>
	</item>
		<item>
		<title>The editor I use</title>
		<link>http://bashinaction.wordpress.com/2009/12/28/the-editor-i-use/</link>
		<comments>http://bashinaction.wordpress.com/2009/12/28/the-editor-i-use/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 10:03:38 +0000</pubDate>
		<dc:creator>thameera</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://bashinaction.wordpress.com/?p=21</guid>
		<description><![CDATA[The editor I use for bash scripting is the VI editor. In fact it is VIM, or Vi IMproved. The other options include gedit, nano and emacs. Any text editor will do the job. You can invoke VI by typing vi in the shell. vi or you can use it followed by the name of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bashinaction.wordpress.com&amp;blog=11064688&amp;post=21&amp;subd=bashinaction&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The editor I use for bash scripting is the VI editor. In fact it is VIM, or Vi IMproved. The other options include gedit, nano and emacs. Any text editor will do the job.</p>
<p style="text-align:center;"><a href="http://bashinaction.files.wordpress.com/2009/12/vi.png"><img class="size-medium wp-image-22 aligncenter" title="VIM screenshot" src="http://bashinaction.files.wordpress.com/2009/12/vi.png?w=300&#038;h=197" alt="VIM screenshot" width="300" height="197" /></a></p>
<p>You can invoke VI by typing <em>vi</em> in the shell.</p>
<pre>   vi</pre>
<p>or you can use it followed by the name of the file you&#8217;re going to edit.</p>
<pre>   vi hello</pre>
<p>VI&#8217;s steep learning curve usually deters many new comers. You have to remember various key commands to perform even simple tasks. However, once you get in touch with the basics, it&#8217;s quite easy to catch up and you will soon find how efficient working with VI is.<br />
VIM is an improved version of VI which is far more intuitive than the former, for example you can use arrow keys to move around the text. It doesn&#8217;t make sense to work with VI when VIM is available. Once you install VIM (<em>sudo apt-get install vim</em> in debian-based distros), just invoking <em>vi</em> instead of <em>vim</em> will take you to VIM.</p>
<p>Two good tutorials for learning VI can be found <a href="http://www.eng.hawaii.edu/Tutor/vi.pdf" target="_blank">here</a> and <a href="http://upload.wikimedia.org/wikibooks/en/0/00/Learning_the_vi_editor.pdf" target="_blank">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bashinaction.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bashinaction.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bashinaction.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bashinaction.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bashinaction.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bashinaction.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bashinaction.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bashinaction.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bashinaction.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bashinaction.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bashinaction.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bashinaction.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bashinaction.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bashinaction.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bashinaction.wordpress.com&amp;blog=11064688&amp;post=21&amp;subd=bashinaction&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bashinaction.wordpress.com/2009/12/28/the-editor-i-use/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/86ce735f59af85b35fa7416c4b330452?s=96&#38;d=monsterid&#38;r=G" medium="image">
			<media:title type="html">thameera</media:title>
		</media:content>

		<media:content url="http://bashinaction.files.wordpress.com/2009/12/vi.png?w=300" medium="image">
			<media:title type="html">VIM screenshot</media:title>
		</media:content>
	</item>
		<item>
		<title>The reverse of the previous simple app</title>
		<link>http://bashinaction.wordpress.com/2009/12/26/the-reverse-of-the-previous-simple-app/</link>
		<comments>http://bashinaction.wordpress.com/2009/12/26/the-reverse-of-the-previous-simple-app/#comments</comments>
		<pubDate>Sat, 26 Dec 2009 06:35:52 +0000</pubDate>
		<dc:creator>thameera</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://bashinaction.wordpress.com/?p=15</guid>
		<description><![CDATA[Remember the bash script I wrote in the previous post? Well, the script below does the exactly opposite. #!/bin/bash #: Description : Reads from a file whose entries are of the  format "name1 name2, age" and writes them in a file line by line #: Author : Thameera #: Date : 26/12/2009 while IFS= read [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bashinaction.wordpress.com&amp;blog=11064688&amp;post=15&amp;subd=bashinaction&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Remember the bash script I wrote in the <a href="http://bashinaction.wordpress.com/2009/12/23/a-simple-app-to-read-from-and-output-to-files/">previous post</a>? Well, the script below does the exactly opposite.</p>
<pre>#!/bin/bash
#: Description : Reads from a file whose entries are of the 
 format "name1 name2, age" and writes them in a file line
 by line
#: Author : Thameera
#: Date : 26/12/2009

while IFS= read -r line
do
 (( ${#line}==0 )) &amp;&amp; exit 0
 nm="${line%,*}"
 printf "%s\n%s\n%s\n" "${nm% *}" "${nm#* }" "${line:${#nm}+2}"
done</pre>
<p>This script widely uses parameter expansion and patterns. For example <span style="font-style:italic;">${line%,*}</span> removes the shortest match for a comma from the end. The percentage sign here means &#8220;remove the shortest match from the end&#8221;. The hash (#) sign used in the next line does the same thing the other way around, i.e. &#8220;remove the shortest match from the beginning&#8221;. The &#8220;shortest match&#8221; means the first match you find when you go from the end or the beginning. If you double the % or #, it will return the longest match.</p>
<p>For example,</p>
<pre>$ var="anaconda"
$ echo ${var##*n}
da</pre>
<p>And the <em>${#nm}</em> expands to the length of the string <em>nm.</em></p>
<p>When the following input is fed to the script:</p>
<pre>chitral gamage, 32
tham sen, 30</pre>
<p>the output is given as follows:</p>
<pre>chitral
gamage
32
tham
sen
30</pre>
<p>So that&#8217;s it. It&#8217;s just another small script, but helped me a lot to understand how parameter expansion works. See you next time with another script where the bash comes in action!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bashinaction.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bashinaction.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bashinaction.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bashinaction.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bashinaction.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bashinaction.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bashinaction.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bashinaction.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bashinaction.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bashinaction.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bashinaction.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bashinaction.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bashinaction.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bashinaction.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bashinaction.wordpress.com&amp;blog=11064688&amp;post=15&amp;subd=bashinaction&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bashinaction.wordpress.com/2009/12/26/the-reverse-of-the-previous-simple-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/86ce735f59af85b35fa7416c4b330452?s=96&#38;d=monsterid&#38;r=G" medium="image">
			<media:title type="html">thameera</media:title>
		</media:content>
	</item>
		<item>
		<title>A simple app to read from and output to files</title>
		<link>http://bashinaction.wordpress.com/2009/12/23/a-simple-app-to-read-from-and-output-to-files/</link>
		<comments>http://bashinaction.wordpress.com/2009/12/23/a-simple-app-to-read-from-and-output-to-files/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 11:16:55 +0000</pubDate>
		<dc:creator>thameera</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://bashinaction.wordpress.com/?p=9</guid>
		<description><![CDATA[This is a script I wrote to get in touch with some bash basics. #!/bin/bash #: Description : Reads a file with entries in separated lines and write them to a file with a comma separated format #: Author : Thameera #: Date : 23/12/2009 while IFS= read -r name1 do read -r name2 read [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bashinaction.wordpress.com&amp;blog=11064688&amp;post=9&amp;subd=bashinaction&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is a script I wrote to get in touch with some bash basics.</p>
<pre>#!/bin/bash
#: Description	: Reads a file with entries in separated
lines and write them to a file with a comma separated format
#:	Author	: Thameera
#: Date	: 23/12/2009

while IFS= read -r name1
do
	read -r	name2
	read -r age
	if [ -z "$age" ]
	then
		break
	fi
	printf "%s %s, %s\n" "$name1" "$name2" "$age"
done</pre>
<p>What this code does is to read from a file specified in the argument list a list with the following format:</p>
<pre>firstname1
lastname1
age1
firstname2
lastname2
age2
....</pre>
<p>and arrange them in the following order in a new file specified by the argument:</p>
<pre>firstname1 lastname1, age1
firstname2 lastname2, age2</pre>
<p>Suppose you save this file with the name <em>main</em> and your input is in the file <em>in</em>. Then the command to execute the script should be:</p>
<pre>bash main &lt;in &gt;out</pre>
<p>This will write the output in a new file called <em>out</em>.</p>
<p>When I first wrote this script, I found out that the output would produce an extra blank line with a comma. The if condition <em>if [ -z "$age" ] </em>was added to rectify this. This condition will break the loop when it finds that the variable <em>age</em> is no longer assigned to a valid value.</p>
<p>Well, it does not do any interesting stuff, but helped me to get touch with some basics. Hope to post some more interesting stuff in the time to come.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bashinaction.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bashinaction.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bashinaction.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bashinaction.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bashinaction.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bashinaction.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bashinaction.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bashinaction.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bashinaction.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bashinaction.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bashinaction.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bashinaction.wordpress.com/9/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bashinaction.wordpress.com/9/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bashinaction.wordpress.com/9/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bashinaction.wordpress.com&amp;blog=11064688&amp;post=9&amp;subd=bashinaction&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bashinaction.wordpress.com/2009/12/23/a-simple-app-to-read-from-and-output-to-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/86ce735f59af85b35fa7416c4b330452?s=96&#38;d=monsterid&#38;r=G" medium="image">
			<media:title type="html">thameera</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello</title>
		<link>http://bashinaction.wordpress.com/2009/12/22/hello/</link>
		<comments>http://bashinaction.wordpress.com/2009/12/22/hello/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 10:42:44 +0000</pubDate>
		<dc:creator>thameera</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://bashinaction.wordpress.com/?p=4</guid>
		<description><![CDATA[Hello there! This is Thameera, a student from Sri Lanka. I had this strange interest in learning shell scripting and have just begun learning it. In this blog, I&#8217;ll post the various bash scripts I write in the time to come, hopefully with explanations. Keep in touch!<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bashinaction.wordpress.com&amp;blog=11064688&amp;post=4&amp;subd=bashinaction&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hello there!</p>
<p>This is Thameera, a student from Sri Lanka. I had this strange interest in learning shell scripting and have just begun learning it. In this blog, I&#8217;ll post the various bash scripts I write in the time to come, hopefully with explanations.</p>
<p>Keep in touch!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bashinaction.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bashinaction.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bashinaction.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bashinaction.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bashinaction.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bashinaction.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bashinaction.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bashinaction.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bashinaction.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bashinaction.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bashinaction.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bashinaction.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bashinaction.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bashinaction.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=bashinaction.wordpress.com&amp;blog=11064688&amp;post=4&amp;subd=bashinaction&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://bashinaction.wordpress.com/2009/12/22/hello/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/86ce735f59af85b35fa7416c4b330452?s=96&#38;d=monsterid&#38;r=G" medium="image">
			<media:title type="html">thameera</media:title>
		</media:content>
	</item>
	</channel>
</rss>
