<?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:media="http://search.yahoo.com/mrss"
	>

<channel>
	<title>*nix Shell</title>
	<atom:link href="http://nixshell.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://nixshell.wordpress.com</link>
	<description>UNIX / Linux Shell Hints and Tips&#160;&#160;&#160;&#160;&#160;&#160;(a http://steve-parker.org/sh/sh.shtml subproject)</description>
	<pubDate>Wed, 23 Apr 2008 00:35:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
	<language>en</language>
			<item>
		<title>Book</title>
		<link>http://nixshell.wordpress.com/2008/04/23/book/</link>
		<comments>http://nixshell.wordpress.com/2008/04/23/book/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 00:35:25 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[Blogroll]]></category>

		<category><![CDATA[MLP]]></category>

		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[bash]]></category>

		<category><![CDATA[files]]></category>

		<category><![CDATA[grep]]></category>

		<category><![CDATA[init]]></category>

		<category><![CDATA[links]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[pipes]]></category>

		<category><![CDATA[regexp]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/?p=66</guid>
		<description><![CDATA[A serious publisher has contacted me about writing a serious book about Linux shell programming.
It is all really very serious. I&#8217;m not used to being serious, as you can probably tell from the fact that I have now used the word &#8220;serious&#8221; four times in this three-sentence post.
I am rather keen to write a book [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A serious publisher has contacted me about writing a serious book about Linux shell programming.</p>
<p>It is all really very serious. I&#8217;m not used to being serious, as you can probably tell from the fact that I have now used the word &#8220;serious&#8221; four times in this three-sentence post.</p>
<p>I am rather keen to write a book on the subject, not because I&#8217;m vain, or desperate for money, but because the stuff I have seen out there in dead-tree format has been of rather low quality. Also because of all the emails I&#8217;ve received over the years, they have all been positive, and none has said anything along the lines of &#8220;I didn&#8217;t need any of that because I bought Book[X]&#8220;, or indeed any book.    People have emailed me, asking for advice as to what book to buy, and I have been unable to recommend any book that I have seen.</p>
<p>So:<br />
<strong><i><br />
What would you like to see in your ideal book about UNIX / Linux shell scripting, be it Bourne, Bash, ksh, tcsh, zsh, whatever?</i></strong></p>
<p>Please don&#8217;t be timid; if you want to know how to work out how many nose-flutes can be fitted into the area of a Boeing 757, you won&#8217;t be anything like as strange as some of the correspondants I&#8217;ve had over the years, so please, tell me what is bugging you, what has bugged you, or even what you think might be likely to bug you in days / months / years to come.</p>
<p>I&#8217;m likely to answer any specific questions here and now, whether or not they end up in the book, but anything you&#8217;d like to see in a book, too&#8230; post that here, and I&#8217;ll have a stab at it.</p>
<p>Also, I would of course be interested to know if you have found any useful books on or around the subject, and what they did particularly well.</p>
<p>Steve</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/66/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/66/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/66/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=66&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2008/04/23/book/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Happy First Birthday!</title>
		<link>http://nixshell.wordpress.com/2008/01/06/happy-first-birthday/</link>
		<comments>http://nixshell.wordpress.com/2008/01/06/happy-first-birthday/#comments</comments>
		<pubDate>Sun, 06 Jan 2008 02:28:24 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[MLP]]></category>

		<category><![CDATA[bash]]></category>

		<category><![CDATA[files]]></category>

		<category><![CDATA[grep]]></category>

		<category><![CDATA[init]]></category>

		<category><![CDATA[links]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[pipes]]></category>

		<category><![CDATA[regexp]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2008/01/06/happy-first-birthday/</guid>
		<description><![CDATA[This blog has now been running for a year; the first post was Hello World on 17th Jan 2007.
I hadn&#8217;t realised it had been going for so long; in that time, I&#8217;ve made 41 posts, so I haven&#8217;t quite managed to make one post per week    I have been a bit slack [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This blog has now been running for a year; the first post was <a href="http://nixshell.wordpress.com/2007/01/17/hello-world/">Hello World</a> on 17th Jan 2007.</p>
<p>I hadn&#8217;t realised it had been going for so long; in that time, I&#8217;ve made 41 posts, so I haven&#8217;t quite managed to make one post per week <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />   I have been a bit slack lately, for which I do apologise. New Years Resolution: I must make more posts here!</p>
<p>In the meantime, my main site, <a href="http://steve-parker.org/">steve-parker.org</a>, has celebrated its seventh birthday, having been born in June 2000 - looking forward to making the 8th birthday celebrations this June!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/65/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/65/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/65/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=65&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2008/01/06/happy-first-birthday/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Ordering items</title>
		<link>http://nixshell.wordpress.com/2007/11/07/ordering-items/</link>
		<comments>http://nixshell.wordpress.com/2007/11/07/ordering-items/#comments</comments>
		<pubDate>Wed, 07 Nov 2007 22:49:57 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[bash]]></category>

		<category><![CDATA[files]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/11/07/ordering-items/</guid>
		<description><![CDATA[There are lots of small little quirks to the *nix shells; this is just one of them.
If you want to list the files in a directory, then ls will list them all for you, in alphabetical order.
If you want to list them by size, you can use ls -S; by timestamp: ls -t, and so [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>There are lots of small little quirks to the *nix shells; this is just one of them.</p>
<p>If you want to list the files in a directory, then <code>ls</code> will list them all for you, in alphabetical order.</p>
<p>If you want to list them by size, you can use <code>ls -S</code>; by timestamp: <code>ls -t</code>, and so on.</p>
<p>But <code>ls</code> is a particular utility. What happens when we do this:</p>
<p><code><br />
for myfile in *<br />
do<br />
&nbsp;&nbsp;echo &#8220;My file is called $myfile&#8221;<br />
done</code></p>
<p>We get an alphabetically sorted list (see <code>man ascii</code> for the actual detail; they&#8217;re sorted by ASCII value, so numbers first, then uppercase letters, then lowercase letters).</p>
<p>This can be a pain, but it can also be quite useful. If you&#8217;ve got a bunch of files:</p>
<p><code>1.install.txt<br />
2.setup.txt<br />
3.use.txt<br />
4.uninstall.txt</code></p>
<p>Then you can play with them in order, just by using the asterisk:</p>
<p><code>for i in *<br />
do<br />
&nbsp;&nbsp;echo &#8220;File $i&#8221; &gt;&gt; all.txt<br />
&nbsp;&nbsp;cat $i &gt;&gt; all.txt<br />
done</code></p>
<p>And it will sort them into order for you (&#8221;1&#8243; comes before &#8220;2&#8243; in ASCII, and so on&#8230;)</p>
<p>Or you could just do this:</p>
<p><code>more * &gt; all.txt</code></p>
<p>Because <code>more</code> will prefix each file with its name in a header, if there is more than one file to process.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/64/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/64/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/64/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=64&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/11/07/ordering-items/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Maths in bash shell</title>
		<link>http://nixshell.wordpress.com/2007/09/28/maths-in-bash-shell/</link>
		<comments>http://nixshell.wordpress.com/2007/09/28/maths-in-bash-shell/#comments</comments>
		<pubDate>Fri, 28 Sep 2007 08:53:04 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/09/28/maths-in-bash-shell/</guid>
		<description><![CDATA[http://snap.nlc.dcccd.edu/reference/bash1/features_29.html has a bash-specific technique for processing calculations:

$[ expression ]
$(( expression ))

See the link for further details.
       ]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://snap.nlc.dcccd.edu/reference/bash1/features_29.html">http://snap.nlc.dcccd.edu/reference/bash1/features_29.html</a> has a bash-specific technique for processing calculations:</p>
<pre>
$[ expression ]
$(( expression ))
</pre>
<p>See the link for further details.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/63/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/63/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/63/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=63&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/09/28/maths-in-bash-shell/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>IFS - Internal Field Separator</title>
		<link>http://nixshell.wordpress.com/2007/09/26/ifs-internal-field-separator/</link>
		<comments>http://nixshell.wordpress.com/2007/09/26/ifs-internal-field-separator/#comments</comments>
		<pubDate>Wed, 26 Sep 2007 00:41:09 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[bash]]></category>

		<category><![CDATA[files]]></category>

		<category><![CDATA[grep]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/09/26/ifs-internal-field-separator/</guid>
		<description><![CDATA[It seems like an esoteric concept, but it&#8217;s actually very useful.
If your input file is &#8220;1 apple steve@example.com&#8221;, then your script could say:
while read qty product customer
do
  echo "${customer} wants ${qty} ${product}(s)"
done
The read command will read in the three variables, because they&#8217;re spaced out from each other.
However, critical data is often presented in spreadsheet [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>It seems like an esoteric concept, but it&#8217;s actually very useful.</p>
<p>If your input file is &#8220;1 apple steve@example.com&#8221;, then your script could say:</p>
<pre>while read qty product customer
do
  echo "${customer} wants ${qty} ${product}(s)"
done</pre>
<p>The <code>read</code> command will read in the three variables, because they&#8217;re spaced out from each other.</p>
<p>However, critical data is often presented in spreadsheet format. If you save these as CSV files, it will come out like this:</p>
<pre>1,apple,steve@example.com</pre>
<p>This contains no spaces, and the above code will not be able to understand it. It will take the whole thing as one item - the first thing, quanity, <code>$qty</code>, and set the other two fields as blank.</p>
<p>The way around this, is to tell the entire shell, that &#8220;,&#8221; (the comma itself) separates fields; it&#8217;s the &#8220;internal field separator&#8221;, or IFS.</p>
<p>The IFS variable is set to space/tab/newline, which isn&#8217;t easy to set in the shell, so it&#8217;s best to save the original IFS to another variable, so you can put it back again after you&#8217;ve messed around with it. I tend to use &#8220;<code>oIFS=$IFS</code>&#8221; to save the current value into &#8220;oIFS&#8221;.</p>
<p>Also, when the IFS variable is set to something other than the default, it can really mess with other code.</p>
<p>Here&#8217;s a script I wrote today to parse a CSV file:</p>
<pre>
#!/bin/sh
oIFS=$IFS     # Always keep the original IFS!
IFS=","          # Now set it to what we want the "read" loop to use
while read qty product customer
do
  IFS=$oIFS
  # process the information
  IFS=","       # Put it back to the comma, for the loop to go around again
done &lt; myfile.txt</pre>
<p>It really is that easy, and it&#8217;s very versatile. You do have to be careful to keep a copy of the original (I always use the name oIFS, but whatever suits you), and to put it back as soon as possible, because so many things invisibly use the IFS - grep, cut, you name it. It&#8217;s surprising how many things within the &#8220;<code>while read</code>&#8221; loop actually did depend on the IFS being the default value.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/62/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/62/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/62/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=62&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/09/26/ifs-internal-field-separator/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Logic</title>
		<link>http://nixshell.wordpress.com/2007/09/07/logic/</link>
		<comments>http://nixshell.wordpress.com/2007/09/07/logic/#comments</comments>
		<pubDate>Fri, 07 Sep 2007 01:00:06 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[MLP]]></category>

		<category><![CDATA[bash]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/09/07/logic/</guid>
		<description><![CDATA[Whilst not directly related to shell programming, understanding of basic logic operations - AND, OR, NOR, XOR, NAND, etc, are as important to shell programmers as to C, Java, .Net and other coders.
My recent interactive logic gate page seems to have become quite popular; it&#8217;s just a simple implementation of each of the major logic [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Whilst not directly related to shell programming, understanding of basic logic operations - AND, OR, NOR, XOR, NAND, etc, are as important to shell programmers as to C, Java, .Net and other coders.</p>
<p>My recent <a href="http://steve-parker.org/logic/">interactive logic gate</a> page seems to have become quite popular; it&#8217;s just a simple implementation of each of the major logic circuits in use. If you want to see more, say so - I&#8217;ll add anything you ask for <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/61/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/61/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/61/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=61&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/09/07/logic/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>25 useful commands in Linux/UNIX for Beginners</title>
		<link>http://nixshell.wordpress.com/2007/08/22/25-useful-commands-in-linuxunix-for-beginners/</link>
		<comments>http://nixshell.wordpress.com/2007/08/22/25-useful-commands-in-linuxunix-for-beginners/#comments</comments>
		<pubDate>Wed, 22 Aug 2007 01:04:03 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[MLP]]></category>

		<category><![CDATA[bash]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/08/22/25-useful-commands-in-linuxunix-for-beginners/</guid>
		<description><![CDATA[The (often a bit geeky for this blog) FreeBSD-World website has a good &#8220;Top-25&#8243; list of 25 useful commands in Linux/UNIX for Beginners
I&#8217;m not sure that #24 (dig) and #25 (host) are absolutely necessary, #18 (startx) is possibly outdated these days, and the compression tools (6-9) are much of a muchness, but apart from that, [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The (often a bit geeky for this blog) FreeBSD-World website has a good &#8220;Top-25&#8243; list of <a href="http://www.freebsd-world.com/Stories/16.php">25 useful commands in Linux/UNIX for Beginners</a></p>
<p>I&#8217;m not sure that #24 (<code>dig</code>) and #25 (<code>host</code>) are absolutely necessary, #18 (<code>startx</code>) is possibly outdated these days, and the compression tools (6-9) are much of a muchness, but apart from that, #1 - #23 should be familiar to anyone who claims to be experienced with UNIX/Linux. If somebody was missing one, it would have to be #18 (<code>startx</code>), as (a) it&#8217;s not needed on servers, and (b) modern *nix distros will boot into a GUI automatically when possible.</p>
<p>So what&#8217;s the list?<br />
25. host<br />
24. dig<br />
23. mkdir<br />
22. rm<br />
21. cp<br />
20. grep<br />
19. ls<br />
18. startx<br />
17. nano / vi<br />
16. pwd<br />
15. cat<br />
14. man<br />
13. kill<br />
12. locate<br />
11. ifconfig<br />
10. ssh<br />
9. gzip<br />
8. bzip2<br />
7. zip<br />
6. tar  (I would put 6-9 in one category, personally. rar should probably be in there too)<br />
5. mount<br />
4. passwd<br />
3. ping<br />
2. tail<br />
1. top</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/60/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/60/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/60/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=60&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/08/22/25-useful-commands-in-linuxunix-for-beginners/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>vi</title>
		<link>http://nixshell.wordpress.com/2007/08/01/vi/</link>
		<comments>http://nixshell.wordpress.com/2007/08/01/vi/#comments</comments>
		<pubDate>Wed, 01 Aug 2007 00:40:10 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/08/01/vi/</guid>
		<description><![CDATA[I use vi every day; to me, there is no better text editor. It is apparently a little intimidating for the newcomer, though&#8230;. here&#8217;s a beginner&#8217;s guide to the VI editor.
I would write one of these, but - much as I love vi, and I really do, everyone who uses vi, seems to have their [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I use vi every day; to me, there is no better text editor. It is apparently a little intimidating for the newcomer, though&#8230;. here&#8217;s a <a href="http://www-acs.ucsd.edu/info/vi_tutorial.shtml">beginner&#8217;s guide to the VI editor</a>.</p>
<p>I would write one of these, but - much as I love vi, and I really do, everyone who uses vi, seems to have their own experience of it, and their own shortcuts. We&#8217;ve all got our own quirks. One thing&#8217;s for sure; vi is not notepad!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/59/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/59/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/59/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/59/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/59/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=59&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/08/01/vi/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Shell Pitfalls</title>
		<link>http://nixshell.wordpress.com/2007/07/30/shell-pitfalls/</link>
		<comments>http://nixshell.wordpress.com/2007/07/30/shell-pitfalls/#comments</comments>
		<pubDate>Mon, 30 Jul 2007 12:40:20 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[MLP]]></category>

		<category><![CDATA[bash]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/07/30/shell-pitfalls/</guid>
		<description><![CDATA[Greg Wooledge has an excellent list of Bash Pitfalls, with good explanations as to why they are wrong, and what the correct syntax should be.
       ]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Greg Wooledge has an excellent list of <a href="http://wooledge.org/mywiki/BashPitfalls">Bash Pitfalls</a>, with good explanations as to why they are wrong, and what the correct syntax should be.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/58/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/58/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/58/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=58&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/07/30/shell-pitfalls/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Understanding init scripts</title>
		<link>http://nixshell.wordpress.com/2007/07/25/understanding-init-scripts/</link>
		<comments>http://nixshell.wordpress.com/2007/07/25/understanding-init-scripts/#comments</comments>
		<pubDate>Wed, 25 Jul 2007 23:47:06 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[bash]]></category>

		<category><![CDATA[files]]></category>

		<category><![CDATA[init]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/07/25/understanding-init-scripts/</guid>
		<description><![CDATA[UNIX and Linux systems use &#8220;init scripts&#8221; - scripts typically placed in /etc/init.d/ which are run when the system starts up and shuts down (or changes runlevels, but we won&#8217;t go into that level of detail here, being more of a sysadmin topic than a shell scripting topic). In a typical setup, /etc/init.d/myservice is linked [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>UNIX and Linux systems use &#8220;init scripts&#8221; - scripts typically placed in <code>/etc/init.d/</code> which are run when the system starts up and shuts down (or changes runlevels, but we won&#8217;t go into that level of detail here, being more of a sysadmin topic than a shell scripting topic). In a typical setup, <code>/etc/init.d/myservice</code> is linked to <code>/etc/rc2.d/S70myservice</code>. That is to say, <code>/etc/init.d/myservice</code> is the real file, but the <code>rc2.d</code> file is a symbolic link to it, called <code>"S70myservice"</code>. The &#8220;S&#8221; means &#8220;Start&#8221;, and &#8220;70&#8243; says when it should be run - lower-numbered scripts are run first. The range is usually 1-99, but there are no rules. <code>/etc/rc0.d/K30myservice</code> (for shutdown), or <code>/etc/rc6.d/K30myservice</code> (for reboot; possibly a different scenario for some services), will be the corresponding &#8220;Kill&#8221; scripts. Again, you can control the order in which your services are shut down; K01* first, to K99* last.</p>
<p>All of these rc scripts are just symbolic links to <code>/etc/init.d/myservice</code>, so there is just one actual shell script, which takes care of starting or stopping the service. The Samba init script from Solaris is a nice and simple script to use as an example:</p>
<pre>
case "$1" in
start)
	[ -f /etc/sfw/smb.conf ] || exit 0

	/usr/sfw/sbin/smbd -D
	/usr/sfw/sbin/nmbd -D
	;;
stop)
	pkill smbd
	pkill nmdb
	;;
*)
	echo &#8220;Usage: $0 { start | stop }&#8221;
	exit 1
	;;
esac
exit 0
</pre>
<p>The <code>init</code> daemon, which controls init scripts, calls a startup script as <code>"/etc/rc2.d/S70myservice start"</code>, and a shutdown script as <code>"/etc/rc0.d/K30myservice stop"</code>. So we have to check the variable <code>$1</code> to see what action we need to take. (See <a href="http://steve-parker.org/sh/variables2.shtml">http://steve-parker.org/sh/variables2.shtml</a> to read about what <code>$1</code> means - in this case, it&#8217;s either &#8220;start&#8221; or &#8220;stop&#8221;).</p>
<p>So we use <a href="http://steve-parker.org/sh/case.shtml">case</a> (follow link for more detail) to see what we are required to do. </p>
<p>In this example, if it&#8217;s &#8220;start&#8221;, then it will run the three commands:</p>
<pre>
	[ -f /etc/sfw/smb.conf ] || exit 0
	/usr/sfw/sbin/smbd -D
	/usr/sfw/sbin/nmbd -D
</pre>
<p>Where line 1 checks that smb.conf exists; there is no point continuing if it doesn&#8217;t exist, just &#8220;exit 0&#8243; (success) so the system continues booting as normal. Lines 2 and 3 start the two daemons required for Samba.</p>
<p>If it&#8217;s &#8220;stop&#8221;, then it will run these two commands:</p>
<pre>
	pkill smbd
	pkill nmdb
</pre>
<p><code>pkill</code> means &#8220;Process Kill&#8221;, and it simply kills off the two processes started by the &#8220;start&#8221; option.</p>
<p>The <code>"*)"</code> construct catches any other uses, and simply replies that the correct syntax is to call it with either &#8220;start&#8221; or &#8220;stop&#8221; - nothing else will do. Some services allow for status reports, restarting, and so on. The one thing we do need to provide is &#8220;start&#8221;. Most services also have a &#8220;stop&#8221; function. All others are optional.</p>
<p>The simplest possible init script would be this, to control an Apache webserver:</p>
<pre>
#!/bin/sh
/usr/sbin/apachectl $1
</pre>
<p>Apache comes with a program called &#8220;apachectl&#8221; (or &#8220;apache2ctl&#8221;), which will take &#8220;stop&#8221; and &#8220;start&#8221; as arguments, and act accordingly. It will also take &#8220;restart&#8221;, &#8220;status&#8221;, &#8220;configtest&#8221;, and a few more options, but that one-line script would be enough to act as /etc/init.d/apache, with /etc/rc2.d/S90apache and /etc/rc0.d/K10apache linking to it. To be frank, even that is not necessary; you could just link <code>/usr/sbin/apachectl</code> into <code>/etc/init.d/apache</code>. In reality, it&#8217;s normally good to provide a few sanity-checks in addition to the basic stop/start functionality.</p>
<p>The vast majority of init scripts use the <a href="http://steve-parker.org/sh/case.shtml">case</a> command; around that, you can wrap all sorts of other things - most GNU/Linux distributions include a generic reporting script (typically <code>/lib/lsb/init-functions</code> - to report &#8220;OK&#8221; or &#8220;FAILED&#8221;), read in a config file (like the Samba example above), define functions for the more involved aspects of starting, stopping, or reporting on the status of the service, and so on. </p>
<p>Some (eg, SuSE) have an &#8220;INIT INFO&#8221; block, which may allow the <code>init</code> daemon a bit more control over the order in which services are started. Ubuntu&#8217;s <a target="_blank" href="http://upstart.ubuntu.com/">Upstart</a> is another; Solaris 10 uses <a target="_blank" href="http://docs.sun.com/app/docs/doc/819-3055/6n5a6lhji?a=view">pmf</a> (Process Monitor Facility), which starts and stops processes, but also monitors them to check that they are running as expected.</p>
<p>After a good decade of stability, in 2007 the world of init scripts appears to be changing, potentially quite significantly. However, I&#8217;m not here to speculate on future developments, this post is just to document the stable interface which is init scripts. Even if other things change, the basic &#8220;start|stop&#8221; syntax is going to be with us for a long time to come. It is easy, but often important, to understand what is going on.</p>
<p>In closing, I will list the run-levels, and what each run-level provides:</p>
<pre>0: Shut down the OS (without powering off the machine)
1, s, S: Single-User mode. Networking is not enabled.
2: Networking enabled (not NFS, Printers)
3: Normal operating mode (including NFS, Printers)
4: Not normally used
5: Shut down the OS and power off the machine
6: Reboot the OS.</pre>
<p>Some GNU/Linux distributions change these definitions - in particular, Debian provides all network services at runlevel 2, not 3. Run-level 5 is also sometimes used to start the graphical (X) interface.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/57/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/57/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/57/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/57/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/57/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=57&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/07/25/understanding-init-scripts/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Shell Pipes by Example</title>
		<link>http://nixshell.wordpress.com/2007/07/22/shell-pipes-by-example/</link>
		<comments>http://nixshell.wordpress.com/2007/07/22/shell-pipes-by-example/#comments</comments>
		<pubDate>Sun, 22 Jul 2007 00:54:31 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[bash]]></category>

		<category><![CDATA[files]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[pipes]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/07/22/shell-pipes-by-example/</guid>
		<description><![CDATA[Pipes, piping, pipelines&#8230; whatever you call them, are very powerful - in fact, they are one of the core tenets of the philosophy behind UNIX (and therefore Linux).  They are also, really, very simple, once you understand them.  The way to understand them, is by playing with them, but if you don&#8217;t know [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Pipes, piping, pipelines&#8230; whatever you call them, are very powerful - in fact, they are one of the core tenets of the philosophy behind UNIX (and therefore Linux).  They are also, really, very simple, <em>once you understand them</em>.  The way to understand them, is by playing with them, but if you don&#8217;t know what they do, you don&#8217;t know where to start&#8230; Catch-22!</p>
<p>So, here are some simple examples of how the pipe works.</p>
<h3>Let&#8217;s see the code</h3>
<p><code>$ grep steve /etc/passwd | cut -d: -f 6<br />
/home/steve<br />
$</code><br />
What did this do? There are two UNIX commands there: <code>grep</code> and <code>cut</code>. The command &#8220;<code>grep steve /etc/passwd</code>&#8221; finds all lines in the file <code>/etc/passwd</code> which contain the text &#8220;steve&#8221; anywhere in the line. In my case, this has one result:<br />
<code>steve:x:1000:1000:Steve Parker,,,:/home/steve:/bin/bash</code><br />
The second command, &#8220;<code>cut -d: -f6</code>&#8221; cuts the line by the delimiter (-d) of a colon (&#8221;<code>:</code>&#8220;), and gets field (-f) number 6. This is, in the <code>/etc/passwd</code> file, the home directory of the user.</p>
<h3>So what? Show me some more</h3>
<p>This is the main point of this article; once you&#8217;ve seen a few examples, it normally all becomes clear.</p>
<h4>EG2</h4>
<pre>$ find . -type f -ls | cut -c14- | sort -n -k 5
rw-r--r--   1 steve    steve       28 Jul 22 01:41 ./hello.txt
rwxr-xr-x   1 steve    steve     6500 Jul 22 01:41 ./a/filefrag
rwxr-xr-x   1 steve    steve     8828 Jul 22 01:42 ./c/hostname
rwxr-xr-x   1 steve    steve    30848 Jul 22 01:42 ./c/ping
rwxr-xr-x   1 steve    steve    77652 Jul 22 01:42 ./b/find
rwxr-xr-x   1 steve    steve    77844 Jul 22 01:41 ./large
rwxr-xr-x   1 steve    steve    93944 Jul 22 01:41 ./a/cpio
rwxr-xr-x   1 steve    steve    96228 Jul 22 01:42 ./b/grep
$</pre>
<p>What I did here, was three commands: &#8220;<code>find . -type f -ls</code>&#8221; finds regular files, and lists them in an &#8220;ls&#8221;-style format: permissions, owner, size, etc.<br />
&#8220;<code>cut -c14-</code>&#8221; cuts out the first 14 characters, which mess up the formatting on this website (!), and aren&#8217;t very interesting.<br />
&#8220;<code>sort -n -k 5</code>&#8221; does a numeric (-n) sort, on field 5 (-k5), which is the size of the file.<br />
So this gives me a list of the files in this directory (and subdirectories), ordered by file size. That&#8217;s much more useful than &#8220;<code>ls -lS</code>&#8220;, which restricts itself to the current directory, but not subdirectories.</p>
<p>(As an aside, I have to admit that I only concocted this by trying to think of an example; it actually seems really useful, and worth making into an alias&#8230; I must do a post about &#8220;<code>alias</code>&#8221; some time!)</p>
<h3>So how does it work?</h3>
<p>This seems pretty straightforward: get lines containing &#8220;steve&#8221; from the input file (&#8221;<code>grep steve /etc/passwd</code>&#8220;), and get the sixth field (where fields are marked by colons) (&#8221;<code>cut -d: -f6</code>&#8220;). You can read the full command from left to right, and see what happens, in that order.</p>
<h3>How does it really work?</h3>
<h4>EG1 Explained</h4>
<p>There are some gotchas when you start to look at the plumbing. Because we&#8217;re using the analogy of a pipe (think of water flowing through a pipe), the OS actually sets up the commands in the reverse order. It calls <code>cut</code>first, then it calls <code>grep</code>. If you have (for example) a syntax error in your <code>cut</code> command, then <code>grep</code> will never be called.<br />
What actually happens is this:</p>
<ol>
<li>A &#8220;pipe&#8221; is set up - a special entity which can take input, which it passes, line by line, to its output.
<li><code>cut</code> is called, and its input is set to be the &#8220;pipe&#8221;.
<li><code>grep</code> is called, and its output is set to be the &#8220;pipe&#8221;.
<li>As <code>grep</code> generates output, it is passed through the pipe, to the waiting <code>cut</code> command, which does its own simple task, of splitting the fields by colons, and selecting the 6th field as output.</ol>
<h4>EG2 Explained</h4>
<p>For EG2, &#8220;<code>sort</code>&#8221; is called first, which ties to the second (rightmost) pipe for its input. Then &#8220;<code>cut</code>&#8221; is called, which ties to the second pipe for its output, and the first (leftmost) pipe for its input. Then, &#8220;<code>find</code>&#8221; is called, which ties to the first pipe for its output.<br />
So, the output of &#8220;<code>find</code>&#8221; is piped into &#8220;<code>cut</code>&#8220;, which strips off the first 14 characters of the &#8220;<code>find</code>&#8221; output. This is then passed to &#8220;<code>sort</code>&#8220;, which sorts on field 5 (of what it receives as input), so the output of the entire pipeline, is a numerically sorted list of files, ordered by size.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/56/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/56/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/56/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/56/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/56/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=56&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/07/22/shell-pipes-by-example/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Shell Cheatsheet</title>
		<link>http://nixshell.wordpress.com/2007/07/14/shell-cheatsheet/</link>
		<comments>http://nixshell.wordpress.com/2007/07/14/shell-cheatsheet/#comments</comments>
		<pubDate>Sat, 14 Jul 2007 00:17:40 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/07/14/shell-cheatsheet/</guid>
		<description><![CDATA[There doesn&#8217;t seem to be any decent shell CheatSheet out there, so I have undertaken to write one.
This is my first attempt; once I actually printed it out, I realised that the the font was actually rather large, so I had room to include much more than I had originally sketched out.
Cheatsheet You may notice [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>There doesn&#8217;t seem to be any decent shell CheatSheet out there, so I have undertaken to write one.</p>
<p>This is my first attempt; once I actually printed it out, I realised that the the font was actually rather large, so I had room to include much more than I had originally sketched out.</p>
<p><a href="http://steve-parker.org/sh/cheatsheet.pdf">Cheatsheet</a> You may notice that there are a lot of blanks;  <a href="http://steve-parker.org/sh/shell_cheatsheet.pdf">Shell Cheatsheet</a> has the same content, with some ideas for new content. Please tell me what you want to see in there  - There is still lots of room, it&#8217;s only about 60% full.</p>
<p>What would you fill the other 40% with?</p>
<p>Does this seem to be a useful cribsheet? What would you like to see in it?</p>
<p>Also, what format would you prefer? PDF? PNG? GIF? DOC?!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/55/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/55/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/55/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=55&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/07/14/shell-cheatsheet/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Termcap</title>
		<link>http://nixshell.wordpress.com/2007/07/13/termcap/</link>
		<comments>http://nixshell.wordpress.com/2007/07/13/termcap/#comments</comments>
		<pubDate>Fri, 13 Jul 2007 01:00:16 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/07/13/termcap/</guid>
		<description><![CDATA[I could talk for hours about TERMCAP, but I won&#8217;t; instead, here&#8217;s a link to one usage of termcap:
&#8216;less&#8217; colours for man pages
The post is interesting, the comments are probably at least as useful as the post.
       ]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I could talk for hours about TERMCAP, but I won&#8217;t; instead, here&#8217;s a link to one usage of termcap:</p>
<p><a href="http://nion.modprobe.de/blog/archives/572-less-colors-for-man-pages.html">&#8216;less&#8217; colours for man pages</a></p>
<p>The post is interesting, the comments are probably at least as useful as the post.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/54/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/54/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/54/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/54/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/54/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=54&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/07/13/termcap/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Positional arguments</title>
		<link>http://nixshell.wordpress.com/2007/07/11/positional-arguments/</link>
		<comments>http://nixshell.wordpress.com/2007/07/11/positional-arguments/#comments</comments>
		<pubDate>Wed, 11 Jul 2007 23:04:48 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/07/11/positional-arguments/</guid>
		<description><![CDATA[A tutorial reader, Maarten Zeinstra, emailed me the other day about Variables - Part II, where I &#34;kinda implicitly state that there are only 9 available params to be given in any program $1..$9 so if you wanted do have more then those you might want to do something like your example. But ${n} also [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A <a href="http://steve-parker.org/sh/sh.shtml">tutorial</a> reader, Maarten Zeinstra, emailed me the other day about <a href="http://steve-parker.org/sh/variables2.shtml">Variables - Part II</a>, where I &quot;kinda implicitly state that there are only 9 available params to be given in any program $1..$9 so if you wanted do have more then those you might want to do something like your example. But ${n} also works to get above 9 params.&quot;</p>
<p>It turns out that this is entirely true - for bash, dash, csh, ksh, and probably other shells too. The ${9} limitation seems only to apply to the traditional Bourne shell - that is, #!/bin/sh on commercial Unices.</p>
<p>If you can be sure that your script will be interpreted by bash, or similar, you can quite happily refer to your tenth argument as ${10}.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/53/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/53/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/53/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=53&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/07/11/positional-arguments/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Interactive Bash keystrokes</title>
		<link>http://nixshell.wordpress.com/2007/06/27/interactive-bash-keystrokes/</link>
		<comments>http://nixshell.wordpress.com/2007/06/27/interactive-bash-keystrokes/#comments</comments>
		<pubDate>Wed, 27 Jun 2007 00:58:08 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/06/27/interactive-bash-keystrokes/</guid>
		<description><![CDATA[Sorry, another link to an external site. I will get some good hints and tips back on here soon, I promise.
In the meantime, here&#8217;s a summary of bash keyboard shortcuts, for those of us who use Bash, be it under Linux or any other OS.
It seems interesting to me, that a few years ago, only [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Sorry, another link to an external site. I will get some good hints and tips back on here soon, I promise.</p>
<p>In the meantime, here&#8217;s a <a href="http://www.howtogeek.com/howto/ubuntu/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc/">summary of bash keyboard shortcuts</a>, for those of us who use Bash, be it under Linux or any other OS.</p>
<p>It seems interesting to me, that a few years ago, only Linux users used Bash (because it was the default, being a GNU shell), whilst now, most UNIX users I know, use Bash (because it&#8217;s so flexible, particularly for interactive use), whilst Ubuntu, the most popular Linux distro, uses dash as /bin/sh. <a href="http://packages.debian.org/unstable/shells/dash">Dash</a> is Posix compliant, but omits a few features that Bash enjoys. Debian notes that &#8220;&#8216;bash&#8217; is a better shell for most users, since it has some nice features absent from &#8216;dash&#8217;, and is a required part of the system.&#8221;</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/52/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/52/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/52/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=52&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/06/27/interactive-bash-keystrokes/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Redirection - Simple Stuff</title>
		<link>http://nixshell.wordpress.com/2007/05/30/redirection-simple-stuff/</link>
		<comments>http://nixshell.wordpress.com/2007/05/30/redirection-simple-stuff/#comments</comments>
		<pubDate>Wed, 30 May 2007 01:11:43 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[bash]]></category>

		<category><![CDATA[files]]></category>

		<category><![CDATA[links]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[regexp]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/05/30/redirection-simple-stuff/</guid>
		<description><![CDATA[Nobody deals with the really low-level stuff any more; I learned it from UNIX Gurus in the 90s. I was really lucky to have met some real experts, and was stupid not to have better understood the opportunity to pick their brains.
Write to a file
$ echo foo &#62; file
Append to a file
$ echo foo &#62;&#62; [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Nobody deals with the really low-level stuff any more; I learned it from UNIX Gurus in the 90s. I was really lucky to have met some real experts, and was stupid not to have better understood the opportunity to pick their brains.</p>
<h2>Write to a file</h2>
<pre>$ echo foo &gt; file</pre>
<h2>Append to a file</h2>
<pre>$ echo foo &gt;&gt; file</pre>
<h2>Read from a file (1)</h2>
<pre>$ cat &lt; file
<h2>Read from a file (2)</h2>
<pre>$ cat file</pre>
<h2>Read lines from a file</h2>
<pre>$ while read f
&gt; do
&gt;   echo LINE: $f
&gt; done &lt; file
$</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/51/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/51/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/51/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=51&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/05/30/redirection-simple-stuff/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Korn Shell Website</title>
		<link>http://nixshell.wordpress.com/2007/05/18/korn-shell-website/</link>
		<comments>http://nixshell.wordpress.com/2007/05/18/korn-shell-website/#comments</comments>
		<pubDate>Fri, 18 May 2007 22:56:16 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[MLP]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/05/18/korn-shell-website/</guid>
		<description><![CDATA[David Korn&#8217;s Korn shell (ksh) lives at http://www.kornshell.com/
I have to admit to a preference for the Bourne shell; partly because of the if/then/else syntax (what is &#8220;[[&#8221; all about?!), partly because /bin/sh is bourne (or compatible) on all traditional UNIX and Linux (where it&#8217;s bash) systems, but ksh is another good, solid shell. The source [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>David Korn&#8217;s Korn shell (ksh) lives at <a href="http://www.kornshell.com/">http://www.kornshell.com/</a></p>
<p>I have to admit to a preference for the Bourne shell; partly because of the if/then/else syntax (what is &#8220;[[&#8221; all about?!), partly because /bin/sh is bourne (or compatible) on all traditional UNIX and Linux (where it&#8217;s bash) systems, but ksh is another good, solid shell. The source is also available from AT&amp;T. It&#8217;s also a nice interactive shell, in the absence of bash.</p>
<p>There&#8217;s also a nice <a href="http://slashdot.org/comments.pl?sid=165237&amp;cid=13788246">anecdotal story about David Korn</a> - at a launch of Microsoft&#8217;s &#8220;Services for UNIX&#8221; (which actually provides UNIX-like services for Windows), I believe.</p>
<p>You can also <a href="http://www.kornshell.com/fun/">see David Korn hanging with KoRn</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/50/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/50/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/50/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=50&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/05/18/korn-shell-website/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Pipes Primer</title>
		<link>http://nixshell.wordpress.com/2007/05/08/pipes-primer/</link>
		<comments>http://nixshell.wordpress.com/2007/05/08/pipes-primer/#comments</comments>
		<pubDate>Tue, 08 May 2007 00:42:03 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[bash]]></category>

		<category><![CDATA[files]]></category>

		<category><![CDATA[grep]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[regexp]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/05/08/pipes-primer/</guid>
		<description><![CDATA[The previous post dealt with pipes, though the example may not have been the best for those who are not accustomed to the concept.
There are a few concepts to be understood - mainly, that of two (or more) processes operating together, how they put their data out, and how the get their data in. UNIX [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The previous post dealt with pipes, though the example may not have been the best for those who are not accustomed to the concept.</p>
<p>There are a few concepts to be understood - mainly, that of two (or more) processes operating together, how they put their data out, and how the get their data in. UNIX deals with multiple processes, all running (conceptually, at least) at the same time, on different CPUs, each with a standard input (stdin), and standard output (stdout). Pipes connect one process&#8217;s stdout to another&#8217;s stdin.</p>
<p>What do we want to pipe? Let&#8217;s say we&#8217;ve got a small 80&#215;25 terminal screen, and lots of files. The <code>ls</code> command will spew out tons of data, faster than we can read it. There&#8217;s a handy utility called &#8220;<code>more</code>&#8220;, which will show a screen-worth of text, then prompt &#8220;more&#8221;. When you hit the space bar, it will scroll down a screen. You can hit ENTER to scroll one line.</p>
<p>I&#8217;m sure that you&#8217;ve worked this out already, but here is how we combine these two commands:</p>
<p><code><br />
$ ls | more<br />
&lt;the first screenful of files is  shown&gt;<br />
&#8211;More&#8211;<br />
</code></p>
<p>What happens here, is that the &#8220;more&#8221; command is started up first, then the &#8220;ls&#8221; command. The output of &#8220;ls&#8221; is piped to the input of &#8220;more&#8221;, so it can read the data.</p>
<p>Most such tools can also work another way, too:<br />
<code><br />
$ more myfile.txt<br />
&lt;the first screenful of &#8220;myfile.txt&#8221; is shown&gt;<br />
&#8211;More&#8211;<br />
</code></p>
<p>That is to say, &#8220;myfile.txt&#8221; is taken as standard input (stdin).</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/49/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/49/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/49/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=49&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/05/08/pipes-primer/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Pipelines in the Shell</title>
		<link>http://nixshell.wordpress.com/2007/05/03/pipelines-in-the-shell/</link>
		<comments>http://nixshell.wordpress.com/2007/05/03/pipelines-in-the-shell/#comments</comments>
		<pubDate>Thu, 03 May 2007 00:13:01 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[bash]]></category>

		<category><![CDATA[files]]></category>

		<category><![CDATA[grep]]></category>

		<category><![CDATA[regexp]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/05/03/pipelines-in-the-shell/</guid>
		<description><![CDATA[One of the most powerful things of the *nix shell, and one which is currently not even covered in the tutorial, is the pipeline. I try to keep this blog and the tutorial from overlapping, but I really must rectify this gap in the main site some time.
In the meantime, this is what it is [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>One of the most powerful things of the *nix shell, and one which is currently not even covered in the <a href="http://steve-parker.org/sh/sh.shtml">tutorial</a>, is the pipeline. I try to keep this blog and the tutorial from overlapping, but I really must rectify this gap in the main site some time.</p>
<p>In the meantime, this is what it is all about.</p>
<p>UNIX (and therefore GNU/Linux) is full of small text-based utilities : <code>wc</code> to count words (and lines, and characters) in a text file; <code>sort</code> to sort a text file; <code>uniq</code> to get only the unique lines from a text file; <code>grep</code> to get certain lines (but not others) from a text file, and so on.</p>
<p>Did you see the common trait there? Yes, it&#8217;s not just that &#8220;Everything is a file&#8221;, nearly everything is also text. It&#8217;s largely from this tradition that HTML, XML, RSS, Email (SMTP, POP, IMAP) and the like are all text-based. Contrast with MS Office, for example, where all data is in binary files which can only (really) be manipulated by the application which created them.</p>
<p>So what? It&#8217;s crude, simple, works on an old-fashioned green-and-black screen. How could that be relevant in the 21st Century?</p>
<p>It&#8217;s relevant, not because of what each tool itself provides, but what they can do when combined.</p>
<h3>Ugly Apache Access Log</h3>
<p>Here&#8217;s a line from my access.log (Apache2) - Yes, honest, it&#8217;s one line. It just looks very ugly:<br />
<code>12.106.111.10 - - [02/May/2007:12:09:58 -0700] &#8220;GET /sh/sh.shtml HTTP/1.1&#8243; 200 33080 &#8220;http://www.google.ca/search?hl=en&amp;q=bourne+shell&amp;btnG=Google+Search&amp;meta=&#8221; &#8220;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3&#8243;</code><br />
That&#8217;s interesting (well, kind of). But it doesn&#8217;t tell us much about this visitor. They came from Google Canada (google.ca), searched for &#8220;bourne shell&#8221;, and clicked on the link to give them http://steve-parker.org/sh/sh.shtml. They&#8217;re using FireFox 1.5.0.3 on Windows.</p>
<p>Great. What happened to them? Did they like the site? Did they stay? Did they actually read anything?</p>
<h3>Filtering it</h3>
<p>We can use <code>grep</code> to filter out just this visitor. However, this gives us lots of stuff we&#8217;re not interested in - all the CSS, PNG, GIF files which support the pages themselves:<br />
<code>$ grep "^12.106.111.10 " access_log<br />
12.106.111.10 - - [02/May/2007:12:09:58 -0700] &#8220;GET /sh/sh.shtml HTTP/1.1&#8243; 200 33080 &#8220;http://www.google.ca/search?hl=en&amp;q=bourne+shell&amp;btnG=Google+Search&amp;meta=&#8221; &#8220;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3&#8243;<br />
12.106.111.10 - - [02/May/2007:12:09:58 -0700] &#8220;GET /steve-parker.css HTTP/1.1&#8243; 200 8757 &#8220;http://steve-parker.org/sh/sh.shtml&#8221; &#8220;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3&#8243;<br />
12.106.111.10 - - [02/May/2007:12:09:59 -0700] &#8220;GET /images/1.png HTTP/1.1&#8243; 200 471 &#8220;http://steve-parker.org/sh/sh.shtml&#8221; &#8220;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3&#8243;<br />
12.106.111.10 - - [02/May/2007:12:09:59 -0700] &#8220;GET /images/prevd.png HTTP/1.1&#8243; 200 1397 &#8220;http://steve-parker.org/sh/sh.shtml&#8221; &#8220;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3&#8243;<br />
12.106.111.10 - - [02/May/2007:12:10:00 -0700] &#8220;GET /images/2.png HTTP/1.1&#8243; 200 648 &#8220;http://steve-parker.org/sh/sh.shtml&#8221; &#8220;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3&#8243;<br />
&#8230; etc &#8230;</code></p>
<p>This is looking ugly already, and not just because of the small width of the webpage - even at 80 characters wide, this is hard to understand.</p>
<h3>Filtering Some More</h3>
<p>At first glance, I should be able to pipe this through a grep for html files:</p>
<pre>$ grep "^12.106.111.10 " access_log | grep shtml</pre>
<p>However, Apache also logs the referrer, so even the &#8220;GET /images/2.png&#8221; request above, includes a .shtml request. So I can use &#8220;<code>grep -v</code>&#8220;. I&#8217;ll add the &#8220;<code>-w</code>&#8221; option to <code>grep</code> to say that the search term must match a whole word. So - &#8220;<code>grep -v gif</code>&#8221; would let &#8220;gifford.html&#8221; through, whereas &#8220;<code>grep -vw gif</code>&#8221; would not. I&#8217;ll add &#8220;\&#8221; to the code, so that you can cut and paste it&#8230; the &#8220;\&#8221; means that although the line breaks, it&#8217;s still really part of the same line of code:<br />
<code>$ grep "^12.106.111.10 " access_log | grep -vw css \<br />
   | grep -vw gif | grep -vw jpg \<br />
   | grep -vw png | grep -vw ico<br />
12.106.111.10 - - [02/May/2007:12:09:58 -0700] &#8220;GET /sh/sh.shtml HTTP/1.1&#8243; 200 33080 &#8220;http://www.google.ca/search?hl=en&amp;q=bourne+shell&amp;btnG=Google+Search&amp;meta=&#8221; &#8220;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3&#8243;<br />
12.106.111.10 - - [02/May/2007:12:10:32 -0700] &#8220;GET /sh/variables1.shtml HTTP/1.1&#8243; 200 48431 &#8220;http://steve-parker.org/sh/sh.shtml&#8221; &#8220;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3&#8243;<br />
12.106.111.10 - - [02/May/2007:12:13:23 -0700] &#8220;GET /sh/external.shtml HTTP/1.1&#8243; 200 41322 &#8220;http://steve-parker.org/sh/sh.shtml&#8221; &#8220;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3&#8243;<br />
12.106.111.10 - - [02/May/2007:12:13:45 -0700] &#8220;GET /sh/quickref.shtml HTTP/1.1&#8243; 200 42454 &#8220;http://steve-parker.org/sh/sh.shtml&#8221; &#8220;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3&#8243;<br />
12.106.111.10 - - [02/May/2007:12:14:27 -0700] &#8220;GET /sh/test.shtml HTTP/1.1&#8243; 200 48844 &#8220;http://steve-parker.org/sh/sh.shtml&#8221; &#8220;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3&#8243;</code><br />
This pumps the output through a grep which gets rid of CSS files (&#8221;<code>| grep -vw css</code>&#8220;), then gif, then jpg, then png, then ico. That should just leave us with the HTML files, which is what we&#8217;re really interested in.</p>
<p>It&#8217;s really hard to see what&#8217;s going on here. The narrow web page doesn&#8217;t help, but we just want to get the key information out, which should be nice and simple. It should look good however we view it.</p>
<p>If you look carefully, you can see that this visitor accessed /sh/sh.shtml, then /sh/variables1.shtml (after a minute), /sh/external.shtml (after 3 minutes), then /sh/quickref.shtml (about 20 seconds later, presumably - given the referrer is the same, in a new tab). A second later, they opened /sh/test.shtml (which also suggests that they&#8217;ve loaded the two pages in tabs, to read at their leisure).</p>
<h3>Getting the Data we need</h3>
<p>However, none of this is really very easy to read. If we just want to know what pages they visited, and when, we need to do some more filtering. <code>awk</code> is a very powerful tool, of whose abilities we will only scratch the surface here. We will get &#8220;fields&#8221; 4 and 7 - the timestamp and the URL accessed.<br />
<code>$ grep "^12.106.111.10 " access_log \<br />
| grep -vw css | grep -vw gif \<br />
| grep -vw jpg | grep -vw png \<br />
| grep -vw ico | awk &#8216;{ print $4,$7 }&#8217;<br />
[02/May/2007:12:09:58 /sh/sh.shtml<br />
[02/May/2007:12:10:32 /sh/variables1.shtml<br />
[02/May/2007:12:13:23 /sh/external.shtml<br />
[02/May/2007:12:13:45 /sh/quickref.shtml<br />
[02/May/2007:12:14:27 /sh/test.shtml</code><br />
Okay, it&#8217;s the info we wanted, but it&#8217;s still not great. That &#8220;[&#8221; looks out of place now. We can use <code>cut</code> to tidy things up. In this case, we&#8217;ll use its positional spacing, because we want to get rid of the first character. Cut&#8217;s &#8220;<code>-c</code>&#8221; paramater tells it what character to cut from. We want the 2nd character onwards, so we just add it to the end of the pipe line:<br />
<code>$ grep "^12.106.111.10 " access_log | grep -vw css | grep -vw gif | grep -vw jpg | grep -vw png | grep -vw ico | awk '{ print $4,$7 }'|cut -c2-<br />
02/May/2007:12:09:58 /sh/sh.shtml<br />
02/May/2007:12:10:32 /sh/variables1.shtml<br />
02/May/2007:12:13:23 /sh/external.shtml<br />
02/May/2007:12:13:45 /sh/quickref.shtml<br />
02/May/2007:12:14:27 /sh/test.shtml</code></p>
<p>And that&#8217;s the kind of thing that we can do with a pipe. We can get exactly what we want from a file.</p>
<h3>Moving on</h3>
<p>At the start of this post, we mentioned <code>sort</code> and <code>uniq</code>. Actually, &#8220;<code>sort -u</code>&#8221; will do the same as &#8220;<code>sort  | uniq</code>&#8220;. So if we want to get the unique visitors, we can just get the first field (&#8221;<code>cut -d" " -f1</code>&#8220;) and sort it uniquely:<br />
<code>$ cut -d" " -f1 access_log | sort -u</code></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/48/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/48/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/48/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=48&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/05/03/pipelines-in-the-shell/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>New Navigation Buttons</title>
		<link>http://nixshell.wordpress.com/2007/04/25/new-navigation-buttons/</link>
		<comments>http://nixshell.wordpress.com/2007/04/25/new-navigation-buttons/#comments</comments>
		<pubDate>Wed, 25 Apr 2007 01:11:20 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/04/25/new-navigation-buttons/</guid>
		<description><![CDATA[Long-lasting readers may have noticed a few minor tweaks over the past week or so to the main site - the background to the main text has been getting lighter, so that it has now actually become white. Also, I have added new navigation buttons:Click to View
What do you think? Are these useful navigation aids, [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Long-lasting readers may have noticed a few minor tweaks over the past week or so to <a href="http://steve-parker.org/sh/sh.shtml">the main site</a> - the background to the main text has been getting lighter, so that it has now actually become white. Also, I have added new navigation buttons:<a href="http://steve-parker.org/images/newlook.html" />Click to View</a></p>
<p>What do you think? Are these useful navigation aids, Do they make the pages too slow to load (there should only be one new 4Kb image per page), or is it much as before?</p>
<p>Any feedback on the new look of <a href="http://steve-parker.org/sh/sh.shtml">the main shell tutorial</a> is, as ever, greatly appreciated.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/47/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/47/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/47/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=47&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/04/25/new-navigation-buttons/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>suid shell scripts - setting &#8220;the SUID bit&#8221;</title>
		<link>http://nixshell.wordpress.com/2007/04/21/suid-shell-scripts-setting-the-sticky-bit/</link>
		<comments>http://nixshell.wordpress.com/2007/04/21/suid-shell-scripts-setting-the-sticky-bit/#comments</comments>
		<pubDate>Sat, 21 Apr 2007 23:58:24 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[MLP]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/04/21/suid-shell-scripts-setting-the-sticky-bit/</guid>
		<description><![CDATA[I just found an excellent explanation of why suid shell scripts are a very bad idea.
So just be glad that most modern OSes don&#8217;t allow it. You really don&#8217;t want to do it, in the first place.
Yes, yes, yes. I know. You do want to do it, and you&#8217;re really really sure (really really really [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I just found an excellent explanation of why suid shell scripts are a <a href="http://www.faqs.org/faqs/unix-faq/faq/part4/section-7.html">very bad idea</a>.</p>
<p>So just be glad that most modern OSes don&#8217;t allow it. You really don&#8217;t want to do it, in the first place.</p>
<p>Yes, yes, yes. I know. You do want to do it, and you&#8217;re really really sure (really really really sure) that it will all be nice and secure; you&#8217;ve got a firewall, completely trusted users, regular password changes, and all the rest of it.</p>
<p>You still don&#8217;t want to do it.</p>
<p>Do it in C - you could even get away with a C wrapper, if you must.</p>
<p>But - and let me be quite clear on this:</p>
<p><strong>Don&#8217;t do it. It really isn&#8217;t worth it, however much it seems, right now, to be a good idea</strong>.</p>
<h3>What is the &#8220;SUID bit&#8221;?</h3>
<p>The suid bit, is what you get from a &#8220;<code>chmod u+s</code>&#8221; command, which will set the permissions to &#8220;-rwsr-xr-x&#8221;, or similar (note the &#8220;s&#8221; replacing the usual &#8220;x&#8221;). That means that you can run certain commands with the permissions of a different user (typically <code>root</code>). This is very useful -for example, a normal user under Linux can&#8217;t open an ICMP socket (see footnote), so they can&#8217;t run the <code>ping</code> command. As a fix, it&#8217;s suid root:</p>
<pre>$ ls -l /bin/ping
-rwsr-xr-x 1 root root 30848 2007-03-05 04:25 /bin/ping
</pre>
<p>okay, it&#8217;s owned by &#8220;root&#8221;, and the SUID bit is set. It will work fine, because <code>ping</code> itself has <code>root</code> permissions:
<pre>$ /bin/ping -c 1 localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.060 ms
--- localhost ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.060/0.060/0.060/0.000 ms</pre>
<p>Good stuff. Now, what if we make a copy? That copy will be owned by a normal user (&#8221;steve&#8221;, in this example), so it won&#8217;t have root permissions:</p>
<pre>$ cp /bin/ping /tmp/
$ ls -l /tmp/ping
-rwsr-xr-x 1 steve steve 30848 2007-04-22 00:46 /tmp/ping
$ /tmp/ping localhost
ping: icmp open socket: Operation not permitted</pre>
<p>It still had the SUID bit set, it&#8217;s just the owner that changed. Let&#8217;s just confirm that it&#8217;s the &#8220;suid + root&#8221; that mattered:
<pre>$ chmod 755 /tmp/ping
$ ls -l /tmp/ping
-rwxr-xr-x 1 steve steve 30848 2007-04-22 00:46 /tmp/ping
$ /tmp/ping localhost
ping: icmp open socket: Operation not permitted</pre>
<p>And what if it&#8217;s owned by <code>root</code>?:
<pre>$ sudo chown root:root /tmp/ping
$ ls -l /tmp/ping
-rwxr-xr-x 1 root root 30848 2007-04-22 00:46 /tmp/ping
$ /tmp/ping localhost
ping: icmp open socket: Operation not permitted
</pre>
<h5>[footnote]: Why can&#8217;t I &#8220;ping&#8221;?</h5>
<p>The reason that Linux <code>ping</code> has to be run as root, isn&#8217;t because <code>ping</code> itself is inherently dangerous, but the things that it does (open network connections, sends data over the network to other machines, and receives data back from them) are potentially dangerous. Therefore, those functions are restricted to the <code>root</code> user. So <code>ping</code> can be seen as a special subset of commands which - whilst they do these potentially dangerous things, do them in a safe and secure manner. This means that the <code>ping</code> command can be elevated to superuser status by the &#8220;SUID bit&#8221;: It&#8217;s a trusted user of certain potentially dangerous commands.</p>
<p>A taxi cab is a useful thing, but it could be used for a ram-raid. The taxi cab, in this case, is like the network protocol. The taxi driver is <code>ping</code>. We trust <code>ping</code> to do the Good Stuff, and not the Bad Stuff. So, we don&#8217;t let anyone get in and start driving the taxi, but we do let anyone get in and ask the taxi driver to take them somewhere. The command &#8220;<code>ping DoS attack www.google.com</code>&#8221; will not work - although another network utility, with the same permissions, may well perform a DoS attach. Similarly, getting into a taxi and telling the driver to &#8220;drive into that electronics store front window, wait for me to pile the car up with goods, and take me to my hiding place&#8221; is not likely to work. If you had a car (had root access) yourself, you could do it, but you can&#8217;t (well, should not be able to) convince these trusted services to do nasty stuff on your behalf.</p>
<p>For that reason, we need to be <i>very</i> picky about what code gets the &#8220;SUID bit&#8221; promotion - the restrictions are there for a reason, so any program with a SUID bit must be able to prove that it will &#8220;only use its powers for good, and not for evil&#8221;, in the good old melodramatic cliche.</p>
<p>Black-hat hackers will spend a lot more time looking at a suid program like <code>ping</code> than at they will at a normal program like <code>cat</code>, even though <code>cat</code> is far more flexible. If <code>mkdir</code> had the SUID bit set, all kinds of mayhem would ensue.</p>
<p>For shell scripts, where security-through-obscurity is not even possible (<code>chmod</code> permissions of <code>r-x</code> are required, at a minimum, so everyone who can run the script, can read exactly how it was written), suid is always to be avoided.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/46/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/46/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/46/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=46&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/04/21/suid-shell-scripts-setting-the-sticky-bit/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Regular Expressions</title>
		<link>http://nixshell.wordpress.com/2007/04/18/regular-expressions/</link>
		<comments>http://nixshell.wordpress.com/2007/04/18/regular-expressions/#comments</comments>
		<pubDate>Wed, 18 Apr 2007 09:20:13 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[grep]]></category>

		<category><![CDATA[links]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[regexp]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/04/18/regular-expressions/</guid>
		<description><![CDATA[http://etext.lib.virginia.edu/services/helpsheets/unix/regex.html has a good introduction to Regular Expressions - grep, sed, and friends. 
It includes a brief discussion on Backreferences (aka &#8220;the stuff that * matched&#8221;)
       ]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://etext.lib.virginia.edu/services/helpsheets/unix/regex.html">http://etext.lib.virginia.edu/services/helpsheets/unix/regex.html</a> has a good introduction to Regular Expressions - grep, sed, and friends. </p>
<p>It includes a brief discussion on Backreferences (aka &#8220;the stuff that * matched&#8221;)</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/45/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/45/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=45&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/04/18/regular-expressions/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Tool Tip: &#8220;Read&#8221; - it does what it says!</title>
		<link>http://nixshell.wordpress.com/2007/04/14/tool-tip-read-it-does-what-it-says/</link>
		<comments>http://nixshell.wordpress.com/2007/04/14/tool-tip-read-it-does-what-it-says/#comments</comments>
		<pubDate>Sat, 14 Apr 2007 00:48:24 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[bash]]></category>

		<category><![CDATA[files]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/04/14/tool-tip-read-it-does-what-it-says/</guid>
		<description><![CDATA[read is a very useful tool; it might seem too simple to bother mentioning, but there are at least three different ways to use it. (Okay, two, and the third isn&#8217;t really anything special about read, just a nifty thing that the shell itself provides)&#8230;
1. Read the whole line
Let&#8217;s start with an interactive script:

$ cat [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><code>read</code> is a very useful tool; it might seem too simple to bother mentioning, but there are at least three different ways to use it. (Okay, two, and the third isn&#8217;t really anything special about <code>read</code>, just a nifty thing that the shell itself provides)&#8230;</p>
<h3>1. Read the whole line</h3>
<p>Let&#8217;s start with an interactive script:</p>
<pre>
$ <b>cat readme.sh</b>
#!/bin/sh
echo &#8220;I&#8217;m a parrot!&#8221;
while read a
do
    echo &#8220;A is $a&#8221;
done
$ <b>./readme.sh</b>
I&#8217;m a parrot!
<b>hello</b>
A is hello
<b>one two three</b>
A is one two three
<b>piglet eeyore pooh owl</b>
A is piglet eeyore pooh owl
<b>^D</b>
$</pre>
<p>Yes, you&#8217;ll need to hit CTRL-D to exit this loop, it&#8217;s just a simple example.</p>
<p>So far, so stupid. But wait; what if I wanted to get that &#8220;one&#8221; &#8220;two&#8221; &#8220;three&#8221; and use them differently?</p>
<h3>2. Read the words</h3>
<pre>
$ <b>cat readme.sh</b>
#!/bin/sh
echo &#8220;I&#8217;m a parrot!&#8221;
while read a b c
do
        echo &#8220;A is $a&#8221;
        echo &#8220;B is $b&#8221;
        echo &#8220;C is $c&#8221;
done
$ <b>./readme.sh</b>
I&#8217;m a parrot!
<b>hello</b>
A is hello
B is
C is
<b>one two three</b>
A is one
B is two
C is three
<b>piglet eeyore pooh owl</b>
A is piglet
B is eeyore
C is pooh owl
<b>^D</b>
$</pre>
<p>So, just by naming some variables, we can pick what we get. And - did you see that last one? We don&#8217;t lose anything, either&#8230; Just because we asked for three variables (a, b, c) and we got 4 values (piglet eeyore pooh owl), we didn&#8217;t lose anything; the last one was treated like a normal read.</p>
<p>This is actually pretty handy stuff; you&#8217;d have to do a bit of messing about with pointers to get the same effect in C, for example.</p>
<h3>3. Read from a file</h3>
<p>We can do all this from a file, too. This isn&#8217;t special to <code>read</code>, but it&#8217;s often used in this way. See that &#8220;while - do - done&#8221; loop? It&#8217;s a sub-shell, and we can direct whatever we want to its input (everything is a file, remember, so the keyboard, a text file, a device driver, whatever you want, it&#8217;s all just a file)</p>
<p>We do this with the &#8220;<code>&lt;</code>&#8221; operator. Just add &#8220;<code>&lt; filename.txt</code>&#8221; after the &#8220;<code>done</code>&#8221; end of the loop:</p>
<pre>
$ <b>cat readme.sh</b>
#!/bin/sh
echo &#8220;I&#8217;m a parrot!&#8221;
while read a b c
do
        echo &#8220;A is $a&#8221;
        echo &#8220;B is $b&#8221;
        echo &#8220;C is $c&#8221;
done  &lt; myfile.txt
$ <b>cat myfile.txt</b>
1 2 3
4
5 6
7
8 9 10 11 12 13
14
15 16 17
$ <b> ./readme.sh</b>
I&#8217;m a parrot!
A is 1
B is 2
C is 3
A is 4
B is
C is
A is 5
B is 6
C is
A is 7
B is
C is
A is 8
B is 9
C is 10 11 12 13
A is 14
B is
C is
A is 15
B is 16
C is 17
</pre>
<p>So we can process tons of data, wherever it comes from.</p>
<h3>4. I only mentioned 3 uses</h3>
<p>We could make the script a bit more useful, by allowing the user to specify the file, instead of hard-coding it to &#8220;<code>myfile.txt</code>&#8220;:</p>
<pre>
$ <b>cat readme.sh</b>
#!/bin/sh
echo &#8220;I&#8217;m a parrot!&#8221;
while read a b c
do
        echo &#8220;A is $a&#8221;
        echo &#8220;B is $b&#8221;
        echo &#8220;C is $c&#8221;
done &lt; $1
$ <b>cat someotherfile.txt</b>
123
1 2 3
one two three four
$ <b>./readme.sh someotherfile.txt</b>
I&#8217;m a parrot!
A is 123
B is
C is
A is 1
B is 2
C is 3
A is one
B is two
C is three four
$</pre>
<h2>Update 14 April</h2>
<p>Updated to fix the &#8220;done &lt; filename.txt&#8221; from the example code of the last two examples.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/44/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/44/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/44/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=44&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/04/14/tool-tip-read-it-does-what-it-says/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>More maths stuff - bc in detail</title>
		<link>http://nixshell.wordpress.com/2007/04/08/more-maths-stuff-bc-in-detail/</link>
		<comments>http://nixshell.wordpress.com/2007/04/08/more-maths-stuff-bc-in-detail/#comments</comments>
		<pubDate>Sun, 08 Apr 2007 00:22:13 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[bash]]></category>

		<category><![CDATA[links]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[shell]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/04/08/more-maths-stuff-bc-in-detail/</guid>
		<description><![CDATA[There&#8217;s a great post about bc at basicallytech.com - I think that I&#8217;ve already covered most of the same ground, but it&#8217;s got lots of great examples.
       ]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>There&#8217;s a <a href="http://www.basicallytech.com/blog/index.php?/archives/23-command-line-calculations-using-bc.html">great post about bc at basicallytech.com</a> - I think that I&#8217;ve already <a href="http://nixshell.wordpress.com/2007/01/29/simple-maths-in-the-unix-shell/">covered</a> <a href="http://nixshell.wordpress.com/2007/03/26/calculating-averages/">most</a> of the same ground, but it&#8217;s got lots of great examples.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/43/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/43/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/43/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=43&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/04/08/more-maths-stuff-bc-in-detail/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
		<item>
		<title>Calculating Averages</title>
		<link>http://nixshell.wordpress.com/2007/03/26/calculating-averages/</link>
		<comments>http://nixshell.wordpress.com/2007/03/26/calculating-averages/#comments</comments>
		<pubDate>Mon, 26 Mar 2007 22:30:27 +0000</pubDate>
		<dc:creator>unixshell</dc:creator>
		
		<category><![CDATA[bash]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://nixshell.wordpress.com/2007/03/26/calculating-averages/</guid>
		<description><![CDATA[The Simple Maths post seems to be the most popular article in the so-far short life of this blog. 
It&#8217;s also something that I have received a few emails about recently, so I feel like posting a bit more on the subject.
I think that the code can speak for itself&#8230; We implement a loop, which [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The <a href="http://nixshell.wordpress.com/2007/01/29/simple-maths-in-the-unix-shell/">Simple Maths</a> post seems to be the most popular article in the so-far short life of this blog. </p>
<p>It&#8217;s also something that I have received a few emails about recently, so I feel like posting a bit more on the subject.</p>
<p>I think that the code can speak for itself&#8230; We implement a loop, which calls the builtin <code>read</code> function (I&#8217;m not sure the &#8220;-p&#8221; flag, to provide a prompt, is universal. It does work with the Bash builtin. If it doesn&#8217;t work on your *nix, it&#8217;s really only for show, so you can live without it.</p>
<p>Because <code>read</code> works on standard input (aka &#8220;stdin&#8221;), it will work interactively from the keyboard, or direct from a file (one number per line).</p>
<p>We use two methods of doing maths in the shell:</p>
<ul>
<li><code>expr</code>, because it&#8217;s a simple and easily-read way to do simple maths: <code>n=`expr $n + 1`</p>
<li><code>bc</code>, because it is more powerful. Do have a play with <code>bc</code> interactively, it can do a lot&#8230; see below.</ul>
<p>So, we can write a fairly simple script (read down, it&#8217;s only actually 11 lines of code without the comments), which is actually quite versatile - it can do running averages, it can be interactive or run from cron, called from another script, even used as a function.</p>
<p>So, here&#8217;s the code. It should be fairly self-explanatory, but do have a look at the interactive <code>bc</code> sample session below, to see what we are doing with <code>bc</code>. Also, do play with <code>bc</code> (some Linux distros have dropped it from the default install recently, so you&#8217;ll have to <code>yast -i bc</code>, or equivalent)</p>
<h3>The Script - Calculate Averages</h3>
<pre>
#!/bin/sh
# Calculate mean (average) of integer data

# Initialise the variables
n=0     # n being the number of (valid) data provided
sum=0   # sum being the running total of all data

# Note that by using ^D (aka "EOF") to quit, this
# script will work just as well interactively, as
# when provided with a file containing the data.
while read -p "Enter a number (^D to quit): " x
do
        # expr is useful for simple maths
  sum=`expr $sum + $x`
  # If this fails, it was non-numeric input
  if [ "$?" -eq "0" ]; then
    # Okay, it was valid input.
    n=`expr $n + 1`
    # We can provide a &#8220;running average&#8221; here;
    # I&#8217;ll comment it out for now.
    # echo &#8220;Running Average:&#8221;
    # echo &#8220;scale=2;$sum/$n&#8221; | bc
    # echo
  fi
done

# Okay, we&#8217;ve done the loop.
# Present the data.
echo &#8220;Overall Average:&#8221;
        # bc is more useful than expr for
        # more involved maths, though its
        # syntax, particularly in a script,
        # is possibly less obvious.
        # Using bc interactively is easier
        # than using it in a shell script
echo &#8220;scale=2;$sum/$n&#8221; | bc
</pre>
<h3>Interactive bc</h3>
<p>The bold text is user input. The rest is from <code>bc</code>:</p>
<table>
<tr>
<td colspan="2">
$ <b>bc</b><br />
bc 1.06<br />
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.<br />
This is free software with ABSOLUTELY NO WARRANTY.<br />
For details type `warranty&#8217;.</td>
</tr>
<tr>
<td><b>ibase=2</td>
<td>I&#8217;ll be entering base2 (binary)</td>
</tr>
<tr>
<td><b>01001001</b></td>
<td>So, I enter 1001001 (73)</td>
</tr>
<tr>
<td>73</td>
<td>And it replies with the answer in base 10</td>
</tr>
<tr>
<td><b>ibase=10</b></td>
<td>Does this set the input base back to 10?</td>
</tr>
<tr>
<td><b>10</b></td>
<td>Let&#8217;s input &#8220;10&#8243;, it should reply &#8220;10&#8243;</td>
</tr>
<tr>
<td>2</td>
<td>No, we entered &#8220;10&#8243; in base 2, which is 2!</td>
</tr>
<tr>
<td><b>ibase=1010</b></td>
<td>So, 10 in binary is 1010 (8+2)</td>
</tr>
<tr>
<td><b>10</b></td>
<td>We say 10</td>
</tr>
<tr>
<td>10</td>
<td>And bc says 10. Good, we&#8217;re back to normal</td>
</tr>
<tr>
<td><b>11</b></td>
<td>And the same for 11</td>
</tr>
<tr>
<td>11</td>
<td>Good, it works. Now for some maths..</td>
</tr>
<tr>
<td><b>1 + 2</b></td>
<td>(tricky stuff!)</td>
</tr>
<tr>
<td>3</td>
<td>Yes, that&#8217;s good, 1+2=3</td>
</tr>
<tr>
<td><b>23 + 34 + 45 + 56</b></td>
<td>We&#8217;re not limited to x+y</td>
</tr>
<tr>
<td>158</td>
<td>So we can build up our sums</td>
</tr>
<tr>
<td><b>10/3</b></td>
<td>10/3 = 3 and a third, right?</td>
</tr>
<tr>
<td>3</td>
<td>Not to 0 decimal places.</td>
</tr>
<tr>
<td><b>scale=2</b></td>
<td>Okay, let&#8217;s have 2 decimal places</td>
</tr>
<tr>
<td><b>10/3</b></td>
<td>Now ask again</td>
</tr>
<tr>
<td>3.33</td>
<td>That&#8217;s better</td>
</tr>
<tr>
<td><b>scale=5</b></td>
<td>Or to 5 points?</td>
</tr>
<tr>
<td><b>10/3</b></td>
<td>Ask again&#8230;</td>
</tr>
<tr>
<td>3.33333</td>
<td>And it works!</td>
</tr>
<tr>
<td><b>scale=1</b></td>
<td>One point: </td>
</tr>
<tr>
<td><b>10/3</b></td>
<td>And ask again</td>
</tr>
<tr>
<td>3.3</td>
<td>As we expected.</td>
</tr>
<tr>
<td><b>scale=0</b></td>
<td>So, scale=0 means 0 places</td>
</tr>
<tr>
<td><b>10/3</b></td>
<td>Should say 3</td>
</tr>
<tr>
<td>3</td>
<td>Yes, we&#8217;re back to where we started.</td>
</tr>
</table>
<h3>Back to the Script</h3>
<p>That made a nice break. Now we&#8217;ll go back to the script&#8230; it&#8217;s only actually 11 lines long:</p>
<pre>
#!/bin/sh
n=0
sum=0
while read x
do
  sum=`expr $sum + $x`
  if [ "$?" -eq "0" ]; then
    n=`expr $n + 1`
  fi
done
echo &#8220;scale=2;$sum/$n&#8221; | bc
</pre>
<p>And as I said, we can use it interactively, or with a file of data:</p>
<pre>
$ cat data.txt
4
5
6
$ average.sh
5.00
</pre>
<p>Because, under *nix, <i>EVERYTHING IS A FILE</i>, even the keyboard!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/nixshell.wordpress.com/42/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/nixshell.wordpress.com/42/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/nixshell.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/nixshell.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/nixshell.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/nixshell.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/nixshell.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/nixshell.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/nixshell.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/nixshell.wordpress.com/42/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/nixshell.wordpress.com/42/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/nixshell.wordpress.com/42/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=nixshell.wordpress.com&blog=691157&post=42&subd=nixshell&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://nixshell.wordpress.com/2007/03/26/calculating-averages/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/unixshell-128.jpg" medium="image">
			<media:title type="html">unixshell</media:title>
		</media:content>
	</item>
	</channel>
</rss>