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

<channel>
	<title>Jabbering Giraffe &#187; sh</title>
	<atom:link href="http://happygiraffe.net/blog/tag/sh/feed/" rel="self" type="application/rss+xml" />
	<link>http://happygiraffe.net/blog</link>
	<description></description>
	<lastBuildDate>Wed, 19 Oct 2011 10:40:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>That Looks Somewhat Iffy</title>
		<link>http://happygiraffe.net/blog/2008/04/13/that-looks-somewhat-iffy/</link>
		<comments>http://happygiraffe.net/blog/2008/04/13/that-looks-somewhat-iffy/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 09:32:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[meme]]></category>
		<category><![CDATA[sh]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2008/04/13/that-looks-somewhat-iffy/</guid>
		<description><![CDATA[Oh, go on, then. % history 1 &#124; awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}'&#124;sort -rn&#124;head 320 git 112 ll 80 cd 48 ls 24 less 20 man 19 happygiraffe.net 17 sudo 15 rm 14 xattr NB: &#8230; <a href="http://happygiraffe.net/blog/2008/04/13/that-looks-somewhat-iffy/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Oh, <a href="http://www.dehora.net/journal/2008/04/10/that-looks-about-right/">go</a> <a href="http://plasmasturm.org/log/497/">on</a>, <a href="http://stuffthathappens.com/blog/2008/04/12/that-looks-completely-wrong/">then</a>.</p>
<pre>
  % history 1 | awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}'|sort -rn|head
  320 git
  112 ll
  80 cd
  48 ls
  24 less
  20 man
  19 happygiraffe.net
  17 sudo
  15 rm
  14 xattr
</pre>
<p><strong>NB:</strong></p>
<ol>
<li>This is at home, not work.  At work, there&#8217;s significantly more <code>git</code>, <code>ant</code>, <code>mvn</code> and <code>mate</code>.</li>
<li>I turn all my ssh known_hosts entries into command line aliases, hence &#8220;happygiraffe.net&#8221;.</li>
<li>I&#8217;ve only just found xattr, so I&#8217;ve been playing with it.</li>
<li>zsh only lists 25 history items by default.  I have to ask it to start from the beginning.</li>
<li>I obviously do too much as root.</li>
</ol>
<p>As always, I can&#8217;t resist tinkering with the shell.  I&#8217;d have written it as <code>history 1 | awk '{print $2}' | sort | uniq -c | sort -rn | head</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2008/04/13/that-looks-somewhat-iffy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shell Scripting</title>
		<link>http://happygiraffe.net/blog/2006/10/05/shell-scripting/</link>
		<comments>http://happygiraffe.net/blog/2006/10/05/shell-scripting/#comments</comments>
		<pubDate>Thu, 05 Oct 2006 10:44:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[sh]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2006/10/05/shell-scripting/</guid>
		<description><![CDATA[I kind of like shell scripting. It&#8217;s quirky, frequently ugly, but it&#8217;s damned useful. Like Perl, it annoys me when I see things which could be written in a better manner. One persistent example is abusing the if statement. if &#8230; <a href="http://happygiraffe.net/blog/2006/10/05/shell-scripting/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I kind of like shell scripting.  It&#8217;s quirky, frequently ugly, but it&#8217;s damned useful.  Like Perl, it annoys me when I see things which <a href="http://tech.randomness.org.uk/index.cgi/id_bash_functions">could be written in a better manner</a>.  One persistent example is abusing the if statement.</p>
<pre>
  if [ "x$machine" = "x" ]
  then
          echo "not in ~/.machines adding"
          echo $1 &gt;&gt; ~/.machines
          source ~/.bash_aliases
  fi
</pre>
<p>What&#8217;s the problem here?  That little &#8220;x&#8221; in the test.  It&#8217;s not necessary and hasn&#8217;t been since the late 80&#8217;s.  There used to be a parsing bug in shells which meant that an empty argument got removed.  So you couldn&#8217;t say <code>[ "$machine" = "" ]</code> because you&#8217;d end up comparing <code>$machine</code> to <code>]</code>.</p>
<p>But this got fixed, a <em>long</em> time ago.  Really.  Unless you&#8217;re working on an aging <a href="http://en.wikipedia.org/wiki/UNIX_System_III">System <span class="caps">III</span></a> box, you shouldn&#8217;t worry about this, it&#8217;s just <a href="http://en.wikipedia.org/wiki/Cargo_cult_programming">Cargo cult programming</a>.</p>
<p>So the test can be just <code>[ "$machine" = "" ]</code>.  That&#8217;s better.  But not good enough.  Testing for an empty string?  The test command (aka <code>[</code>) provides a <code>-z</code> operator for that!  So goes down to <code>[ -z "$machine" ]</code>.</p>
<p>In this particular case, we&#8217;re explicitly targetting bash, though.  One of the nice features of modern <span class="caps">POSIX</span> shells like bash is that they provide <a href="http://www.gnu.org/software/bash/manual/bashref.html#SEC77">conditional expressions</a>, which have different parsing rules.  So you can say <code>[[ -z $machine ]]</code> and not worry about the lack of quotes.</p>
<p>Lastly, I should point out that I missed off the first line of that extract.</p>
<pre>
  machine=`grep $1 ~/.machines`
</pre>
<p>With that in place, you can see that we&#8217;re not actually using <code>$machine</code> anywhere in the function.  It&#8217;s purely in place for the <code>if</code> statement.  Which means you can simplify this even further.  <code>if</code> takes a command as it&#8217;s argument.  So why not just give it the grep command?</p>
<pre>
  if grep -q $1 ~/.machines
  then
    echo "not in ~/.machines adding"
    echo $1 &gt;&gt; ~/.machines
    source ~/.bash_aliases
  fi
</pre>
<p>I had to add the -q flag to shut grep up; we&#8217;re only interested in the return code, not the output.</p>
<p>Does this matter?  Probably not much.  But like all programming languages, it&#8217;s a lot easier to read when you use it idiomatically.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2006/10/05/shell-scripting/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Quote of the day</title>
		<link>http://happygiraffe.net/blog/2006/03/15/quote-of-the-day/</link>
		<comments>http://happygiraffe.net/blog/2006/03/15/quote-of-the-day/#comments</comments>
		<pubDate>Wed, 15 Mar 2006 10:54:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[quote]]></category>
		<category><![CDATA[sh]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2006/03/15/quote-of-the-day/</guid>
		<description><![CDATA[From the mod_ssl configure script: Programming in Bourne-Shell is a higher form of masochism. No known attribution. This still doesn&#8217;t explain why I enjoy shell scripting so much.]]></description>
			<content:encoded><![CDATA[<p>From the <a href="http://www.modssl.org/">mod_ssl</a> configure script:</p>
<blockquote>
<p>Programming in Bourne-Shell is a higher form of masochism.</p>
</blockquote>
<p>No known attribution.</p>
<p>This still doesn&#8217;t explain why I enjoy shell scripting so much.  <code> <img src='http://happygiraffe.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </code></p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2006/03/15/quote-of-the-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

