<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>code monk</title>
	<atom:link href="http://drj11.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://drj11.wordpress.com</link>
	<description>hacking habits</description>
	<lastBuildDate>Tue, 21 May 2013 21:49:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='drj11.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>code monk</title>
		<link>http://drj11.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://drj11.wordpress.com/osd.xml" title="code monk" />
	<atom:link rel='hub' href='http://drj11.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Date formats</title>
		<link>http://drj11.wordpress.com/2013/04/12/date-formats/</link>
		<comments>http://drj11.wordpress.com/2013/04/12/date-formats/#comments</comments>
		<pubDate>Fri, 12 Apr 2013 10:19:25 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dates]]></category>
		<category><![CDATA[iso8601]]></category>
		<category><![CDATA[times]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=1003</guid>
		<description><![CDATA[Hilary Mason on twitter bemoaned the fact that matplotlib appears to use a floating point number of days to represent datetimes and suggested that &#8220;any other standard format&#8221; would be better. It is a little bit odd that a Python library uses that format, but it&#8217;s presumably because matlab does, and matplotlib is betraying its [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=1003&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="https://https://twitter.com/hmason/">Hilary Mason</a> on twitter <a href="https://twitter.com/hmason/status/297467083665584129">bemoaned the fact that matplotlib appears to use a floating point number of days to represent datetimes</a> and suggested that &#8220;any other standard format&#8221; would be better. It is a little bit odd that a Python library uses that format, but it&#8217;s presumably because matlab does, and matplotlib is betraying its matlab heritage.</p>
<p>What other standards are there? Well, there&#8217;s POSIX time_t which is either an integer or a floating-point type (but actual practice seems to favour integer), and stores the number of seconds since the Unix Epoch, 1970-01-01T00:00:00Z. Not counting leap seconds. Not counting leap seconds is convenient for some calculations, but means there are (recent) times that cannot be represented (namely, any moment during a leap second). That&#8217;s not a good representation of time.</p>
<p>There&#8217;s another time format in use which is confusingly similar to POSIX&#8217;s time_t: it counts time using the number of seconds since the Unix Epoch. Including leap seconds. The opportunities for mistakes in conversion are endless. A problem with this time format is that times in the future (more than a couple of years into the future XX) are ambiguous. Since we don&#8217;t yet know if there are going to be leap seconds between now and 2020 we don&#8217;t know whether 2020-01-01T00:00:00Z is 1577836800 or a few seconds more or less than that.</p>
<p>An example confusion: &#8220;man 2 time&#8221; on my Linux box claims that time_t is the number of seconds since Epoch, but &#8220;date &#8211;date=2013-01-01 +%s&#8221; ends in &#8220;00&#8243; so clearly leap seconds aren&#8217;t being accounted for.</p>
<p>That&#8217;s twice now that I&#8217;ve used ISO 8601: 2013-04-09T06:06:06Z is roughly when I&#8217;m writing this paragraph. As a format for representing this times this has obvious benefits and equally obvious drawbacks. The benefit is that it&#8217;s pretty clear, and almost human readable (by humans like me). A drawback is that it takes a lot of space: 20 characters as I&#8217;ve given it. If you were storing this in a file you could reasonably use the &#8220;compact&#8221; form: 20130409060606Z, which is 15 characters, or 14 without the &#8216;Z&#8217;. Space misers could encode this in BCD and fit it in 8 bytes, which is no more than a 64-bit time_t.</p>
<p>Another drawback with ISO 8601 is that calculations with times can be a bit trickier. But on the other hand, you&#8217;re more likely to get correct answers, and be able to represent all (recent and future) times.</p>
<p>I&#8217;ve been working with sea-level and tide data recently and Hilary&#8217;s floating point number of days is certainly pretty convenient for that. The calculations are concerned with the average rotation of the earth, and it nicely represents that. Practitioners of the art also seem to like a slightly different time scale which is Julian centuries. Basically the same but scaled by 36525. It&#8217;s not unusual for the 0-point to be something like 1900-01-01T00:00:00, or even a midday like 1899-12-31T12:00:00.</p>
<p>What about representing times with sub-second accuracy? When Unix time_t type is an integer type then you&#8217;re stuck. ISO 8601 times can be extended with a decimal point and have as many decimal places as you like. So that&#8217;s a good representation. Hilary&#8217;s floating point number of days is also pretty good, as long as you&#8217;re using double precision. If you&#8217;re using double precision then even counting Julian centuries is okay, you still get sub-nanosecond precision. JavaScript&#8217;s representation is basically a floating point number that is the Unix time_t but in milliseconds not seconds. That too is pretty good.</p>
<p>I think Hilary&#8217;s format is actually pretty reasonable. Some calculations (involving counting whole numbers of days between times) are easier, some are tricky (but no trickier than any other format really). All times are representable. There&#8217;s some ambiguity about times in the future (that 1200 appointment you make in the year 2020 might actually turn up in your calendar at 12:00:00.5 if there is a leap second, but at least your midnight appointments won&#8217;t be on the wrong day). Yes, it&#8217;s a litle bit funky that on days with leap seconds the step between consecutive seconds will be different (it will in 1/86359 or 1/86401 instead of 1/86400), but I&#8217;m sure you&#8217;ll cope.</p>
<p>After a while, you just get used to seeing a new file format, another new date representation. According to the <a href="https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966">xlrd documentation</a> Excel uses a floating point number of days since 1899-12-30T00:00:00, but since they thought 1900 was a leap year, this is only reliable after 1900-03-01. Unless the Excel file was made on a Mac, in which case the Epoch is 1904-01-01. The <a href="http://sealevel.colorado.edu/files/2013_rel2/sl_ns_global.txt">TOPEX/Jason data for global mean sea level</a> represents dates as a decimal fraction of years.</p>
<p>The bottom line is: There are all sorts of crazy date formats. Get over it.</p>
<p>I recommend:</p>
<p>1) using a good library.<br />
2) for storage, use ISO 8601<br />
3) for runtime/manipulation use double.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/1003/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/1003/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=1003&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2013/04/12/date-formats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>All I need to know to learn R</title>
		<link>http://drj11.wordpress.com/2012/10/05/all-i-need-to-know-to-learn-r/</link>
		<comments>http://drj11.wordpress.com/2012/10/05/all-i-need-to-know-to-learn-r/#comments</comments>
		<pubDate>Fri, 05 Oct 2012 13:01:45 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=993</guid>
		<description><![CDATA[I&#8217;ve been learning R, mostly because it&#8217;s been on my list of things to do for ages, and partly because I needed to draw a histogram. All the tedious stuff about how you get started and how you install things is surprisingly difficult to get from the internets. So to install on Ubunutu: Then you [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=993&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been learning R, mostly because it&#8217;s been on my list of things to do for ages, and partly because I needed to draw a histogram.</p>
<p>All the tedious stuff about how you get started and how you install things is surprisingly difficult to get from the internets.</p>
<p>So to install on Ubunutu:</p>
<pre class="brush: bash; title: ; notranslate">
sudo apt-get install r-base
</pre>
<p>Then you run the command R:</p>
<pre class="brush: bash; title: ; notranslate">
$ R
</pre>
<p>Once you&#8217;re in R you can find things out by using Google.  Once you&#8217;ve found a function you want to use, say <a href="http://rss.acs.unt.edu/Rdoc/library/lawstat/html/sj.test.html">sj.test</a>, if it doesn&#8217;t seem to be installed, you can install it by noting the library name which is in curly brackets at the top of the man page.  {lawstat} in this case.  So you then go:</p>
<pre class="brush: r; title: ; notranslate">
&gt; install.packages(&quot;lawstat&quot;) # to install it
&gt; library(lawstat) # to use it
</pre>
<p>(the package installer has a hilariously craptastic interface written in Tcl/Tk)</p>
<p>That&#8217;s it.  Everything I need to know to learn R.  Everything else is just bog standard programming language stuff (though it helps a bit that I learnt a bit of J).</p>
<p>Here&#8217;s the histogram:</p>
<p><a href="http://drj11.files.wordpress.com/2012/10/station-dmet.png"><img src="http://drj11.files.wordpress.com/2012/10/station-dmet.png?w=480&#038;h=480" alt="" title="My First R Histogram" width="480" height="480" class="alignleft size-medium wp-image-999" /></a></p>
<p>And the <a href="https://github.com/drj11/dtr/blob/4f78aa444f7fd284a3b42c387a146329e856a05d/hist.R">R code is behind this link</a>.  The way the function <em>png()</em> implicitly makes <em>hist()</em> write into the PNG file is particularly bletcherous.  It has all the elegance of writing JCL for IBM mainframes.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/993/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/993/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=993&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2012/10/05/all-i-need-to-know-to-learn-r/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>

		<media:content url="http://drj11.files.wordpress.com/2012/10/station-dmet.png?w=480" medium="image">
			<media:title type="html">My First R Histogram</media:title>
		</media:content>
	</item>
		<item>
		<title>Bosch: the Constructor</title>
		<link>http://drj11.wordpress.com/2012/05/17/bosch-the-constructor/</link>
		<comments>http://drj11.wordpress.com/2012/05/17/bosch-the-constructor/#comments</comments>
		<pubDate>Thu, 17 May 2012 19:50:33 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[aleatoric]]></category>
		<category><![CDATA[duplo]]></category>
		<category><![CDATA[lego]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=972</guid>
		<description><![CDATA[We got some second-hand Duplo recently, and washed it. In Bosch, our washing machine. In a pillow case at 30°C on the delicate program. Makes a funny noise but it all came out fine. This was the first time I&#8217;d washed Lego in a machine, so it&#8217;s nice to know that the suggestion in the [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=972&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>We got some second-hand Duplo recently, and washed it.  In Bosch, our washing machine.  In a pillow case at 30°C on the <em>delicate</em> program.  Makes a funny noise but it all came out fine.  This was the first time I&#8217;d washed Lego in a machine, so it&#8217;s nice to know that the <a href="http://www.multicon.de/fun/legofaq.html#a9">suggestion in the Lego FAQ</a> works.</p>
<p>The curious thing was that the Duplo had SELF ASSEMBLED.  It went in all in separated bricks.  But when it comes out, some of it has stuck together.  I found this pretty amazing.  Surely if Lego can self assemble in the washing machine then it is a simple step from there to the beginning of life itself.</p>
<p>The variety of forms is interesting.  Simple diatoms&#8230; in mixed colours:</p>
<p><a href="http://drj11.files.wordpress.com/2012/05/diatom-duo.jpg"><img src="http://drj11.files.wordpress.com/2012/05/diatom-duo.jpg?w=300&#038;h=140" alt="" title="diatom-duo" width="300" height="140" class="alignleft size-medium wp-image-973" /></a></p>
<p style="clear:both;">
&#8230;and also monochrome:</p>
<p><a href="http://drj11.files.wordpress.com/2012/05/diatom-mono.jpg"><img src="http://drj11.files.wordpress.com/2012/05/diatom-mono.jpg?w=300&#038;h=225" alt="" title="diatom-mono" width="300" height="225" class="alignleft size-medium wp-image-975" /></a></p>
<p style="clear:both;">Simple towers, reminiscent of the things my little nephew makes, when he can be bothered:</p>
<p><a href="http://drj11.files.wordpress.com/2012/05/tower.jpg"><img src="http://drj11.files.wordpress.com/2012/05/tower.jpg?w=280&#038;h=300" alt="" title="tower" width="280" height="300" class="alignleft size-medium wp-image-980" /></a></p>
<p style="clear:both;">Then there comes the forms that are not so easy to characterise.  I like to call them monsters:</p>
<p><a href="http://drj11.files.wordpress.com/2012/05/monster.jpg"><img src="http://drj11.files.wordpress.com/2012/05/monster.jpg?w=300&#038;h=211" alt="" title="monster" width="300" height="211" class="alignleft size-medium wp-image-982" /></a></p>
<p style="clear:both;">
I&#8217;m particularly impressed with Bosch&#8217;s creative instincts here.  The one at the front is a battleship with effective use of colour.  The back left reminds me a bit of a tree.  Perhaps some sort of highly coloured baobab tree.  And is that a red boot kicking a yellow football on a field of green?</p>
<p><a href="http://www.flourish.org/blog/">Francis</a> speculates (whimsically) that maybe my washing machine has passed the singularity and is in fact an Artifical Intelligence trying to communicate with me.  Obviously it is constrained by only being able to communicate by rearranging whatever I put into the washing machine.  And the fact that it is a 4-bit microcontroller attached only to a valve, a heater, and a motor (maybe a temperature sensor too, but I wouldn&#8217;t be surprised if there wasn&#8217;t one).  It&#8217;s an intriguing possibility.  Perhaps the form on the right of <em>monsters</em> represents a not quite complete utterance, the Duplo bricks not quite bonded properly.</p>
<p>The <em>AI hypothesis</em> raises several questions: How can we test it? Can we distinguish between merely aleatoric arrangements, and intentional ones? What is Bosch trying to say? Is there something I can put in the washing machine that would make it easier for Bosch to communicate? Is Bosch happy?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/972/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/972/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=972&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2012/05/17/bosch-the-constructor/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>

		<media:content url="http://drj11.files.wordpress.com/2012/05/diatom-duo.jpg?w=300" medium="image">
			<media:title type="html">diatom-duo</media:title>
		</media:content>

		<media:content url="http://drj11.files.wordpress.com/2012/05/diatom-mono.jpg?w=300" medium="image">
			<media:title type="html">diatom-mono</media:title>
		</media:content>

		<media:content url="http://drj11.files.wordpress.com/2012/05/tower.jpg?w=280" medium="image">
			<media:title type="html">tower</media:title>
		</media:content>

		<media:content url="http://drj11.files.wordpress.com/2012/05/monster.jpg?w=300" medium="image">
			<media:title type="html">monster</media:title>
		</media:content>
	</item>
		<item>
		<title>In praise of ⌘</title>
		<link>http://drj11.wordpress.com/2012/05/04/in-praise-of-%e2%8c%98/</link>
		<comments>http://drj11.wordpress.com/2012/05/04/in-praise-of-%e2%8c%98/#comments</comments>
		<pubDate>Fri, 04 May 2012 12:09:09 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[command key]]></category>
		<category><![CDATA[windows key]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=961</guid>
		<description><![CDATA[Control-C is the intr character in Unix.  It sends SIGINT to the currently running process, thereby interrupting it.  Most programs either quit or return to an interactive prompt when they receive this signal. Control-C is the Copy command in Windows.  It copies the highlighted text (or other graphic objects) to the clipboard. What do you do [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=961&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Control-C is the <em>intr</em> character in Unix.  It sends SIGINT to the currently running process, thereby interrupting it.  Most programs either quit or return to an interactive prompt when they receive this signal.</p>
<p>Control-C is the <em>Copy</em> command in Windows.  It copies the highlighted text (or other graphic objects) to the clipboard.</p>
<p>What do you do when you&#8217;re using a terminal emulation program on Windows to connect to a Unix computer?  Does pressing Control-C send a Control-C character to the Unix system, or does it copy the highlighted text to the clipboard?  I have no idea (it&#8217;s been a long while since I used a terminal emulator on Windows), but one thing is clear: It&#8217;s not clear what the right answer is.</p>
<p>On a Mac <em>Copy</em> is an example of a command to a GUI application.  Keyboard shortcuts are accessed using the Command key.</p>
<div id="attachment_963" class="wp-caption alignleft" style="width: 330px"><a href="http://en.wikipedia.org/wiki/File:Apple_key.jpg"><img class="size-full wp-image-963" title="Command Key" src="http://drj11.files.wordpress.com/2012/05/command.png?w=380" alt="Command Key"   /></a><p class="wp-caption-text">Command Key from wikimedia</p></div>
<p style='clear:left;' />
<p>This is a stroke of genius.</p>
<p>Now, when using a terminal emulation program on a Mac, it&#8217;s completely obvious what happens.  Control-C sends a Control-C character to the Unix system, which will interpret it as <em>intr</em>.  Command-C copies the highlight text to the clipboard.</p>
<p>Was this luck?  Hard to tell, but I think not.  The Mac was conceived as an entire product.  It therefore seems completely reasonable to design a keyboard just for that product, and completely reasonable to create a new key for doing Mac things.  Windows was conceived as an operating system.  It had to run on existing hardware.</p>
<p>Why didn&#8217;t Apple just stick a Control key on the keyboard and use that?  Well, that would have been silly, and difficult to explain.  Control had an existing use (on a terminal to send control characters that generally affected either the connection or the movement of the print head) which had nothing to do with GUI commands.  Better to name the key that is used for keyboard shortcuts for GUI commands, Command.</p>
<p>Why didn&#8217;t Windows do this?  Microsoft weren&#8217;t in a position where they could dictate what keys appeared on the keyboard.  They had to make do with whatever was there.  Why Control-C for Copy instead of Alt-C, say?  I have no idea.  It seems particularly mysterious since, at the time Windows was layered on top of DOS and so was used by people familiar with DOS and in DOS, at least at the command prompt, Control-C meant interrupt, like it does in Unix.</p>
<p>The lucky bit seems to be that Apple ever put a Control key on the keyboard at all; early keyboards didn&#8217;t have one, but I guess as soon as you have a modem you can connect to some other computer and that will require a Control key for some things.  But basically the Control key on a Mac hung around for 2 decades doing nothing much until OS X comes out and then it&#8217;s used in all the traditional Unix ways.</p>
<p>What did Unix do when it got a window system?  Well, on X Windows in the mid nineties I don&#8217;t remember using a keyboard shortcut for <em>Copy</em>.  In fact, I don&#8217;t remember using <em>Copy</em> at all.  The middle mouse button (most Unix workstations and X servers of the time had mice with 3 or more buttons) would paste the highlighted text into whichever window the mouse was pointing at (note: the highlighted text could be in a different window from the one which receives the pasted text).  No separate <em>Copy</em> command, just highlight text in one window, and click middle button to paste it into another window.  Cute.</p>
<p>Windows has a Windows button now (and, much to my surprise has had it since Windows 95).  Obviously there was no way that <em>Copy</em> could be moved from Control-C to Windows-C because Control-C was already welded into the minds of all Windows users.  So the Windows button generally does <a href="http://en.wikipedia.org/wiki/Windows_key#Microsoft_Windows_shortcuts">useless things in Windows</a>.</p>
<p>What about Ubuntu?  They could&#8217;ve decided that it was reasonable to assume that all keyboards would have the Windows key.  They could&#8217;ve decided that all GUI shortcut commands would use the Windows key.  Freeing up Control-C to mean <em>intr</em>.  You would&#8217;ve though that for a GUI running on Unix this would be an important consideration.  But no, Ubuntu thoughtlessly copies windows and Control-C means <em>Copy</em>.  Unless you&#8217;re in the terminal emulator, Terminal, in which case it sends a Control-C character and you have to remember to use Shift-Control-C for <em>Copy</em>.  And just like Windows, the Windows key is useless in Ubuntu.</p>
<p>It all just seems like a lost opportunity to me.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/961/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/961/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=961&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2012/05/04/in-praise-of-%e2%8c%98/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>

		<media:content url="http://drj11.files.wordpress.com/2012/05/command.png" medium="image">
			<media:title type="html">Command Key</media:title>
		</media:content>
	</item>
		<item>
		<title>Making change with shell</title>
		<link>http://drj11.wordpress.com/2012/03/13/making-change-with-shell/</link>
		<comments>http://drj11.wordpress.com/2012/03/13/making-change-with-shell/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 08:56:26 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[/bin/sh]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[functional programming]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=952</guid>
		<description><![CDATA[I was flicking through Wikström&#8217;s «Functional Programming Using Standard ML», when I noticed he describes the problem of making up change for an amount of money m using coins of certain denominations (18.1.2, page 233). He says we &#8220;want a function change that given an amount finds the smallest number of coins that adds to [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=952&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I was flicking through Wikström&#8217;s «Functional Programming Using Standard ML», when I noticed he describes the problem of making up change for an amount of money <var>m</var> using coins of certain denominations (18.1.2, page 233).  He says we &#8220;want a function <var>change</var> that given an amount finds the smallest number of coins that adds to that amount&#8221;, and &#8220;Obviously, you first select the biggest possible coin&#8221;.  Here&#8217;s his solution in ML:</p>
<pre class="brush: plain; title: ; notranslate">
exception change;
fun change 0 cs = nul
  | change m nil = raise change
  | change m (ccs as c::cs) = if m &gt;= c
      then c::change (m-c) ccs
      else change m cs;
</pre>
<p>It&#8217;s pretty neat.  The recursion proceeds by either reducing the magnitude of the first argument (the amount we are giving change for), or reducing the size of the list that is the second argument (the denominations of the coins we can use); so we can tell that the recursion must terminate.  Yay.</p>
<p>It&#8217;s not right though.  Well, it gives correct change, but it doesn&#8217;t necessarily find the solution with fewest number of coins.  Actually, it depends on the denominations of coins in our currency; probably for real currencies the &#8220;biggest coin first&#8221; algorithm does in fact give the fewest number of coins, but consider the currency used on the island of <var>san side-effect</var>, the <var>lambda</var>.  <var>lambdas</var> come in coins of Λ1, Λ10, and Λ25 (that&#8217;s not a wedge, it&#8217;s a capital lambda.  <a href="http://drj11.wordpress.com/2008/02/11/abuses-of-lambda-by-design/">It&#8217;s definitely not a fake A</a>).</p>
<p>How do we give change of Λ30? { Λ10, Λ10, Λ10 } (3 tens); what does Wikström&#8217;s algorithm give? 1 twenty-five and 5 ones.  Oops.</p>
<p>I didn&#8217;t work out a witty solution to the fewest number of coins change, but I did create, in shell, a function that lists all the possible ways of making change.  Apart from trivial syntactic changes, it&#8217;s not so different from the ML:</p>
<pre class="brush: bash; title: ; notranslate">
_change () {
    # $1 is the amount to be changed;
    # $2 is the largest coin;
    # $3 is a comma separated list of the remaining coins

    if [ &quot;$1&quot; -eq 0 ] ; then
        echo ; return
    fi
    if [ -z &quot;$2&quot; ] ; then
        return
    fi
    _change $1 ${3%%,*} ${3#*,}
    if [ &quot;$1&quot; -lt &quot;$2&quot; ] ; then
        return
    fi
    _change $(($1-$2)) $2 $3 |
        while read a ; do
            echo $2 $a
        done
}

change () {
    _change $1 ${2%%,*} ${2#*,},
}
</pre>
<p>Each solution is output as a single line with the coins used in a space separated list.  <var>change</var> is a wrapper around <var>_change</var> which does the actual work.  The two base cases are basically identical: «&#8221;$1&#8243; -eq 0» is when we have zero change to give, and we output an empty line (just a bare <tt>echo</tt>) which is our representation for the empty list; «-z &#8220;$2&#8243;» is when the second argument (the first element of the list of coins) is empty, and, instead of raising an exception, we simply return without output any list at all.</p>
<p>The algorithm to generate all possible combinations of change is only very slightly different from Wikström&#8217;s: if we can use the largest coin, then we generate change both without using the largest coin (first recursive call to <tt>_change</tt>, on line 12) and using the largest coin (second recursive call to <tt>_change</tt>, on line 16).  See how we use a <var>while</var> loop to prepend (cons, if you will) the largest coin value to each list returned by the second recursive call.  Of course, when the largest coin is too large then we proceed without it, and we only have the first recursive call.</p>
<p>The list of coins is managed as two function arguments. $2 is the largest coin, $3 is a comma separated list of the remaining coins (including a trailing comma, added by the wrapping <var>change</var> function).  See how, in the first recursive call to <var>_change</var> $3 is decomposed into a head and tail with ${3%%,*} and ${3#*,}.  As hinted at <a href="http://drj11.wordpress.com/2012/03/05/taking-the-bash-out-of-mark/">in the previous article</a>, «%%» is a greedy match and removes the largest suffix that matches the pattern «,*» which is everything from the first comma to the end of the string, and so it leaves the first number in the comma separated list.  «#» is a non-greedy match and removes the smallest prefix that matches the pattern «*,», so it removes the first number and its comma from the list.  Note how I am assuming that all the arguments do not contain spaces, so I am being very cavalier with double quotes around my $1 and $2 and so on.</p>
<p>It even works:</p>
<pre>
$ change 30 25,10,1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
10 10 1 1 1 1 1 1 1 1 1 1
10 10 10
25 1 1 1 1 1
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/952/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/952/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=952&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2012/03/13/making-change-with-shell/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>Taking the bash out of Mark</title>
		<link>http://drj11.wordpress.com/2012/03/05/taking-the-bash-out-of-mark/</link>
		<comments>http://drj11.wordpress.com/2012/03/05/taking-the-bash-out-of-mark/#comments</comments>
		<pubDate>Mon, 05 Mar 2012 08:59:05 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[/bin/sh]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=947</guid>
		<description><![CDATA[Mark Dominus, in his pretty amusing article about exact rational arithmetic in shell gives us this little (and commented!) shell function: Since I&#8217;m on a Korn overdrive, what would this look like without the bashisms? Dominus uses BASH_REMATCH to split a decimal fraction at the decimal point, thus splitting &#8216;fff.iii&#8217; into &#8216;fff&#8217; and &#8216;iii&#8217;. That [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=947&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Mark Dominus, in his <a href="http://blog.plover.com/prog/spark.html">pretty amusing article about exact rational arithmetic in shell</a> gives us this little (and commented!) shell function:</p>
<pre class="brush: bash; title: ; notranslate">
        # given an input number which might be a decimal, convert it to
        # a rational number; set n and d to its numerator and
        # denominator.  For example, 3.3 becomes n=33 and d=10;
        # 17 becomes n=17 and d=1.
        to_rational() {
          # Crapulent bash can't handle decimal numbers, so we will convert
          # the input number to a rational
          if [[ $1 =~ (.*)\.(.*) ]] ; then
              i_part=${BASH_REMATCH[1]}
              f_part=${BASH_REMATCH[2]}
              n=&quot;$i_part$f_part&quot;;
              d=$(( 10 ** ${#f_part} ))
          else
              n=$1
              d=1
          fi
        }
</pre>
<p>Since I&#8217;m on a Korn overdrive, what would this look like without the bashisms?  Dominus uses <var>BASH_REMATCH</var> to split a decimal fraction at the decimal point, thus splitting &#8216;fff.iii&#8217; into &#8216;fff&#8217; and &#8216;iii&#8217;.  That can be done using portable shell syntax (that is, blessed by the Single Unix Specification) using the &#8216;%&#8217; and &#8216;#&#8217; features of parameter expansion.  Example:</p>
<pre>
$ f=3.142
$ echo ${f%.*}
3
$ echo ${f#*.}
142
</pre>
<p>In shell, «${f}» is the value of the variable (parameter) <var>f</var>; you probably knew that.  «${f%pattern}» removes any final part of <var>f</var> that matches <var>pattern</var> (which is a shell pattern, not a regular expression).  «${f#pattern}» removes any initial part of <var>f</var> that matches <var>pattern</var> (full technical details: they remove the shortest match; use %% and ## for greedy versions).</p>
<p>Thus, between them «${f%.*}» and «${f#*.}» are the integer part and fractional part (respectively) of the decimal fraction.  The only problem is when the number has no decimal point.  Well, Dominus special cased that too.  Of course the &#8220;=~&#8221; operator is a bashism (did perl inspire bash, or the other way around?), so portable shell programmers have to use &#8216;case&#8217; (which traditionally was always preferred even when &#8216;[' could be used because 'case' didn't fork another process).  At least this version features a secret owl hidden away (on line 3):</p>
<pre class="brush: bash; title: ; notranslate">
to_rational () {
  case $1 in
    (*.*) i_part=${1%.*} f_part=${1#*.}
      n=&quot;$i_part$f_part&quot;
      d=$(( 10 ** ${#f_part} )) ;;
    (*) n=$1 d=1 ;;
  esac
}
</pre>
<p>The '**' in the arithmetic expression raised a doubt in my mind and, *sigh*, it turns out that it's not portable either (it does work in 'ksh', but it's not in the Single Unix Specification).  Purists have to use a while loop to add a '0' digit for every digit removed from <var>f_part</var>:</p>
<pre class="brush: bash; title: ; notranslate">
to_rational () {
  case $1 in
    (*.*) i_part=${1%.*} f_part=${1#*.}
      n=&quot;$i_part$f_part&quot;
      d=1;
      while [ -n &quot;${f_part}&quot; ] ; do
          d=${d}0
          f_part=${f_part%?}
      done ;;
    (*) n=$1 d=1 ;;
  esac
}
</pre>
<p>Traditional shell didn't support this «${f%.*}» stuff, but it's been in Single Unix Specification for ages.  It's been difficult to find a Unix with a shell that didn't support this syntax since about the year 2000.  It's time to start to be okay about using it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/947/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/947/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=947&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2012/03/05/taking-the-bash-out-of-mark/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>Interactive Shells and their prompts</title>
		<link>http://drj11.wordpress.com/2012/03/04/interactive-shells-and-their-prompts/</link>
		<comments>http://drj11.wordpress.com/2012/03/04/interactive-shells-and-their-prompts/#comments</comments>
		<pubDate>Sun, 04 Mar 2012 20:46:49 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[/bin/sh]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=941</guid>
		<description><![CDATA[What can we say about what the &#8220;-i&#8221; option to shell does? It varies according to the shell. Below, I start with PS1 set to &#8220;demo$ &#8220;, which is not my default. (you can probably work it out from the transcript, but) it might help to know that my default shell is bash (for now). [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=941&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>What can we say about what the &#8220;-i&#8221; option to shell does?  It varies according to the shell.  Below, I start with <var>PS1</var> set to &#8220;demo$ &#8220;, which is not my default.  (you can probably work it out from the transcript, but) it might help to know that my default shell is <var>bash</var> (for now).  There&#8217;s nothing special about &#8216;pwd&#8217; in the examples below, it&#8217;s just a command with a short name that outputs something.</p>
<pre class="brush: bash; title: ; notranslate">
demo$ echo pwd | ksh
/home/drj/hackdexy/content
demo$ echo pwd | bash
/home/drj/hackdexy/content
demo$ echo pwd | ksh -i
$ /home/drj/hackdexy/content
$ 
demo$ echo pwd | bash -i
drj$ pwd
/home/drj/hackdexy/content
drj$ exit
demo$ echo pwd | bash --norc -i
bash-4.2$ pwd
/home/drj/hackdexy/content
bash-4.2$ exit
demo$ echo pwd | sh -i
$ /home/drj/hackdexy/content
$ 
sh: Cannot set tty process group (No such process)
demo$ . ../bin/activate
(hackdexy)demo$ echo pwd | ksh -i
(hackdexy)demo$ /home/drj/hackdexy/content
(hackdexy)demo$ 
(hackdexy)demo$ deactivate
</pre>
<p>What have we learnt?  (when stdin is not a terminal) shells do not issue prompts unless the &#8220;-i&#8221; option is used.  That&#8217;s basically what &#8220;-i&#8221; does.  <var>bash</var>, but not <var>ksh</var>, will echo the command.  That has the effect of making bash&#8217;s output seem more like an interactive terminal session.</p>
<p>Both &#8216;bash&#8217; and &#8216;ksh&#8217; changed the prompt.  &#8216;bash&#8217; changed the prompt because it sources my &#8216;.bashrc&#8217; and that sets <var>PS1</var>; &#8216;ksh&#8217; changed my prompt, apparently because <var>PS1</var> is not an exported shell variable, and so &#8216;ksh&#8217; does not inherit <var>PS1</var> from its parent process, and so sets it to the default of &#8220;$ &#8220;.  If we stop &#8216;bash&#8217; from sourcing &#8216;.bashrc&#8217; by using the &#8216;&#8211;norc&#8217; option (&#8216;&#8211;posix&#8217; will do as well) then it too will use its default prompt: the narcissistic &#8220;bash-4.2$ &#8220;.</p>
<p>&#8216;sh&#8217; (dash on my Ubuntu laptop, apparently), is like &#8216;ksh&#8217; in that it does not echo the commands.  But it does output a narked warning message.</p>
<p>The last command, after using &#8216;activate&#8217; from <var><a href="http://pypi.python.org/pypi/virtualenv">virtualenv</a></var>, demonstrates that &#8216;activate&#8217; must export <var>PS1</var>.  I think that is a bug, but I would welcome comment on this matter.</p>
<p>I guess most people use &#8216;bash&#8217; and mostly set <var>PS1</var> their &#8216;.bashrc&#8217;, so wouldn&#8217;t notice any of this subtlety (for example, they will not notice that &#8216;activate&#8217; exports <var>PS1</var> because the &#8216;.bashrc&#8217; will set it to something different).</p>
<p>I note that the example login and ENV scripts given in <em>The KornShell Command and Programming Language</em> (p241 1989 edition) set <var>PS1</var> is the login script only, and do not export it.  Meaning that subshells will have the default prompt.  I quite like that (it also means that subshells from an &#8216;activate&#8217;d shell will have the &#8216;activate&#8217; prompt).  Why doesn&#8217;t anyone do it like that any more?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/941/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/941/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=941&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2012/03/04/interactive-shells-and-their-prompts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>The 64-bit revolution</title>
		<link>http://drj11.wordpress.com/2012/02/03/the-64-bit-revolution/</link>
		<comments>http://drj11.wordpress.com/2012/02/03/the-64-bit-revolution/#comments</comments>
		<pubDate>Fri, 03 Feb 2012 11:13:22 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=936</guid>
		<description><![CDATA[Basically, wtf happened here? It&#8217;s 2012 and I&#8217;m chastised (a bit) on Twitter for installing 64-bit Ubuntu on my 64-bit laptop. It&#8217;s 1993 and as a graduate student in the Computer Lab I get an account on the shared Unix server. A DEC Alpha, running OSF/1. The Alpha was a true 64-bit chip, no 32-bit [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=936&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Basically, wtf happened here?  It&#8217;s 2012 and I&#8217;m chastised (a bit) <a href="https://twitter.com/#!/sleeveroller/status/147720793449697280">on Twitter</a> for installing 64-bit Ubuntu on my 64-bit laptop.</p>
<p>It&#8217;s 1993 and as a graduate student in the Computer Lab I get an account on the shared Unix server.  A DEC Alpha, running OSF/1.  The Alpha was a true 64-bit chip, no 32-bit heritage in its history, no 32-bit mode, and basically no 32-bit arithmetic instructions.  Everything was 64-bit, no hostages.  As a C programmer this is quite funny.  <var>int</var> was still 32-bit (with the usual compiler flags).  It was clear that the Alpha with its elegant, very RISC, instruction set was an example a new breed of processor architecture.</p>
<p>In 1994 I started my first job and in the next few years I use and program a variety of 64-bit architectures.  The Alpha, UltraSPARC, 64-bit MIPS; I read up on various others (HP PA-RISC 2.0, POWER/PowerPC), because it was clear that the future was one where everyone had a 64-bit RISC workstation on their desk.  Unix was clearly the only operating system with a chance of running on 64-bit architectures; sure, I saw, and used briefly, Windows NT running on Alpha, but it was a sorry effort.  A 32-bit port of NT onto a 64-bit architecture.  Microsoft/Intel were barely making it on 32-bit architectures, never mind 64-bit.</p>
<p>When Intel&#8217;s IA-64 architecture came out, I read the manuals.  It was clear that: a) Intel hadn&#8217;t been paying attention; and, b) no-one would be able to write a decent C compiler for this architecture.</p>
<p>What happens next?  I really don&#8217;t know.  Sun got sucked into Java and took their eye off the ball, every other manufacturer canned their 64-bit line or sold to someone who didn&#8217;t care, Intel&#8217;s efforts to actually <em>implement</em> their IA-64 architecture (Itanium) were indeed a massive failure, and Intel had to piss away the R&amp;D for 2 or 3 entire generations of CPU design.  The amazing thing is that even though Intel basically failed to innovate for like a decade (between Pentium Pro and Core) everyone worshipped them and gave Intel enough time (aka money) to steal their new 64-bit strategy from their competitor AMD.  They had the DEC designers, and the fabs, and they eventually produced Core, which was actually pretty good from a hardware perspective.  But still based on the awful Intel software architecture.</p>
<p>Which is where we are now.</p>
<p>Does anyone have any idea why all the very sensible and nice 64-bit architectures (SPARC, Alpa, MIPS, PowerPC, not PA-RISC) all failed basically together?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/936/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/936/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=936&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2012/02/03/the-64-bit-revolution/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>Unix command line pebbles</title>
		<link>http://drj11.wordpress.com/2012/01/13/unix-command-line-pebbles/</link>
		<comments>http://drj11.wordpress.com/2012/01/13/unix-command-line-pebbles/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 11:31:37 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[/bin/sh]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=922</guid>
		<description><![CDATA[I always find that &#8220;sitting next to someone new&#8221; is an interesting and revealing experience. I learn new things, they learn new things. Over the years I&#8217;ve accumulated a bunch of tricks about using the command line, using vi, and even using emacs. The thing I&#8217;m talking about are things can&#8217;t really be learned in [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=922&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I always find that &#8220;sitting next to someone new&#8221; is an interesting and revealing experience.  I learn new things, they learn new things.  Over the years I&#8217;ve accumulated a bunch of tricks about using the command line, using vi, and even using emacs.  The thing I&#8217;m talking about are things can&#8217;t really be learned in a formal context, they are too small and there are too many of them.  Yet collectively they form part of the fabric of what it means to be a vi user or a shell programmer or whatever.  Picking them up and passing them on by sitting next to someone is the only way I know to spread them.  Well, maybe this blog post is a start.</p>
<p>Here&#8217;s a few I&#8217;ve recently picked up or passed on:</p>
<p><code>cd -</code></p>
<p>It changes directory to the last directory that you changed from.  It&#8217;s /bin/bash not /bin/sh (so I personally would avoid writing it in scripts).  I think someone alleged that this was undocumented, but in fact I recently checked and it is in fact documented in the <em>builtins</em> section of the /bin/bash man page, which is huge.</p>
<p>It&#8217;s useful when you want to cd to some directory to run a command, but then cd back.</p>
<p>Line 25 of the <a href="https://bitbucket.org/ScraperWiki/scraperwiki/src/b30ef424dce1/uml/runlocal#cl-25">runlocal script in ScraperWiki</a> used to do just that in order to start a service from within a different current directory:</p>
<pre class="brush: plain; title: ; notranslate">
cd ../services/scriptmgr
node ./scriptmgr.js &amp;
cd -
</pre>
<p>Because I don&#8217;t like writing bash-specific scripts, <a href="https://bitbucket.org/ScraperWiki/scraperwiki/src/32fec230c0cd/uml/runlocal#cl-30">I changed this to use a subshell, with round brackets</a>:</p>
<pre class="brush: plain; title: ; notranslate">
(
    cd ../services/scriptmgr
    node ./scriptmgr.js &amp;
)
</pre>
<p>A subshell is used to run the code between the round brackets, and it is just a forked copy of the shell.  It has exactly the same environment (environment variables, open files, and so on), but is in another process.  Since the Current Working Directory is specific to the process, changing it in the subshell has no effect on the outer shell, which carries on unaffected after the subshell has returned.  So that&#8217;s something I&#8217;ve passed on recently.</p>
<p><code>cp foo.cnf{-inhg,}</code></p>
<p>This is the same as <code>cp foo.cnf-inhg foo.cnf</code>.  It copies a file by removing the <em>-inhg</em> extension from its name.  ScraperWiki stores some config files in Mercurial (the <em>-inhg</em> versions), and they are copied so they can be edited locally (the versions without the <em>-inhg</em> suffix).  I&#8217;m sure this has all sorts of evil uses.</p>
<p><code>sudo !!</code></p>
<p>Of the things I&#8217;ve picked up recently, this is my favourite.  I like how some of these tips are a combination of things that I already knew individually but hadn&#8217;t thought to combine.  Of course I know that <em>sudo thing</em> runs <em>thing</em> as root, and I did once know that <em>!!</em> is the /bin/csh mechanism (stolen by /bin/bash) for executing the previously typed command.  In combination, <em>sudo !!</em> runs the previously typed command as root.  Which is perfect for when you typed <em>pip install thinginator</em> instead of <em><u>sudo</u> pip install thinginator</em>.</p>
<p><code>Ctrl-R</code></p>
<p>The favourite thing I seem to have spread seems to be Ctrl-R.  (in /bin/bash) you press Ctrl-R, start typing, and bash will search for what you type in your command history; press Return to execute the command or Ctrl-R to find the next older match.  Press Ctrl-C if you didn&#8217;t really want to do that.</p>
<p>Credits</p>
<p>One of the @pysheff crowd for <em>sudo !!</em>; can&#8217;t remember whether it was <a href="https://twitter.com/#!/davbo">@davbo</a> or <a href="https://twitter.com/#!/dchetwynd">@dchetwynd</a>.</p>
<p><a href="https://twitter.com/#!/rossjones">Ross Jones</a> for <em>cd -</em>;</p>
<p>Bitbucket user <a href="https://bitbucket.org/mammadori">mammadori</a> &#8220;{-ext,}&#8221;.</p>
<p>If you enjoyed this post, you may also like <a href="http://blog.plover.com/prog/bash-expr.html">Mark Dominus&#8217; semi-rant on the craptastic way to do calculation in shell scripts</a>.  Huh, now I feel compelled to that Mark gets it wrong about GNU Shell; arithmetic expansion is not a GNU innovation, it <a href="http://pubs.opengroup.org/onlinepubs/007908799/xcu/chap2.html#tag_001_006_004">comes from POSIX</a>, having been slightly mutated from an earlier Korn Shell feature.  And if he had comments I would&#8217;ve said that on his &#8220;blag&#8221;.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/922/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/922/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=922&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2012/01/13/unix-command-line-pebbles/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
		<item>
		<title>My Favourite Bugs, Part 2</title>
		<link>http://drj11.wordpress.com/2011/05/12/my-favourite-bugs-part-2/</link>
		<comments>http://drj11.wordpress.com/2011/05/12/my-favourite-bugs-part-2/#comments</comments>
		<pubDate>Thu, 12 May 2011 11:16:16 +0000</pubDate>
		<dc:creator>drj11</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[bugs]]></category>

		<guid isPermaLink="false">http://drj11.wordpress.com/?p=912</guid>
		<description><![CDATA[Another one. Two of us were working at the client&#8217;s site on the software for what was essentially a compact static robot that was responsible for moving small items from a hopper to a delivery point. There was approximately one instance of the hardware that we were writing software for, and it sat on a [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=912&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Another one.</p>
<p>Two of us were working at the client&#8217;s site on the software for what was essentially a compact static robot that was responsible for moving small items from a hopper to a delivery point.  There was approximately one instance of the hardware that we were writing software for, and it sat on a bench between our two workstations.  Most of the mechanical bits were in a more or less finished state.  I was particularly impressed with the piston that generated partial vacuum so that items could be picked with a moving arm with suction cups on.  Just one or two gears were made of prototyping plastic; and because of a gearing problem the belt didn&#8217;t move at the speed that it said it should in the spec.  But you know, typical prototype hardware.  The electronics were a mixture of off-the-shelf dev kits for 8-bit embedded micros, mini custom circuit boards for novel sensors, and lovingly hand soldered discrete parts.  Add to that the fact that as a software guy I didn&#8217;t really understand the importance of grounding, and <em>La Machine</em> wasn&#8217;t always completely reliable (my colleague had just lent me Tracy Kidder&#8217;s <em>Soul of a New Machine</em>).</p>
<p>Various optical/IR sensors kept track of the items as they moved inside the internals of the machine, various other sensors kept track of motor positions and/or speeds.  There was a slightly hairy state machine (documented using OmniGraffle) to keep track of it all.  The target pick rate was 5 items per second, and as it took more than 200ms for an item to go from the hopper to the point where it left the machine, there could be several items &#8220;in-flight&#8221; at any one time (and of course, picking an item was never completely reliable, so the sensors were used to track the items, and determine if a retry was required).</p>
<p>This day it seemed to be working fine, except for some reason the software was reporting that items were failing to be delivered, when in fact I could plainly see that items were popping out of the top of the machine.  This was causing the machine to prematurely stop, as it would, sensibly, stop picking items if it thought that a picked item was stuck inside the machine somewhere.  Up to this point it had basically been working fine; it had been working the same morning. I was sat thinking about this and investigating somewhat.  I&#8217;d even checked the last thing I&#8217;d changed.  So I called my colleague over (just at the next desk) and he came over to look while I demonstrated the problem.  It worked.  There was no problem.  Flakies (there&#8217;s a memorable part of Kidder&#8217;s book where one of the engineers in helping a more junior engineer sort out a problem with a wire-wrap memory board, grabs the whole frame and shakes it, claiming that it&#8217;s probably just <em>flakies</em>; of course it works after that).</p>
<p>So that&#8217;s okay then.  But when I try it again, it&#8217;s not working.  Colleague comes over.  Works.  I work at it on my own.  Doesn&#8217;t work.</p>
<p>It turned out that it was quite a sunny day.  The sun reached the window in the afternoon.  Sunlight was falling on machine near the optical sensor and presumably bouncing around enough to prevent the sensor from registering the occlusion as the item went past.  When my colleague came over, he was standing over the machine and his shadow blocked the sunlight.  This wouldn&#8217;t be a problem in the production hardware, as it was all in a box (and hence, dark inside).</p>
<p>I constructed an optical shield and installed it on <em>La Machine</em> (a post-it note stuck to the side).</p>
<p>This was not a problem that could be fixed using print.  We did have print via the serial port, but printing more than one character was hazardous because it meant that time taken to transmit characters on the serial line would interfere with the realtime operation of the rest of the software (when items are being picked every 200ms, every millisecond counts).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/drj11.wordpress.com/912/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/drj11.wordpress.com/912/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=drj11.wordpress.com&#038;blog=258145&#038;post=912&#038;subd=drj11&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://drj11.wordpress.com/2011/05/12/my-favourite-bugs-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/9c6dfaac50b9c43815dd18081e87f3e3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drj11</media:title>
		</media:content>
	</item>
	</channel>
</rss>
