<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

	<title>Comments on: No more shell games!</title>
	<link>http://ask.metafilter.com/39266/No-more-shell-games/</link>
	<description>Comments on Ask MetaFilter post No more shell games!</description>
	<pubDate>Wed, 31 May 2006 17:11:36 -0800</pubDate>
	<lastBuildDate>Wed, 31 May 2006 17:11:36 -0800</lastBuildDate>
	<language>en-us</language>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<ttl>60</ttl>

	<item>
		<title>Question: No more shell games!</title>
		<link>http://ask.metafilter.com/39266/No-more-shell-games</link>	
		<description>*nix shell &quot;best practices&quot; anyone? &lt;br /&gt;&lt;br /&gt; Over the years I&apos;ve been writing more and more shell scripts (bash, ksh, perl, expect) and they&apos;ve been getting more and more complexe.  I haven&apos;t been able to find a comprehensive list of best practices for shell programming although I have learned a lot by looking at other people&apos;s scripts (thank you sanity checks!)  I&apos;m looking for links or your own best practices.  &lt;br&gt;
&lt;br&gt;
Here&apos;s an example:&lt;br&gt;
&lt;br&gt;
&lt;small&gt;Use &lt;code&gt;${FOO}&lt;/code&gt; instead of &lt;code&gt;$FOO&lt;/code&gt; so that the variable is more likely to be interpreted when concatenated in a string and use &lt;code&gt;&amp;gt;&amp;gt; &quot;${FOO}&quot;&lt;/code&gt; instead of &lt;code&gt;&amp;gt;&amp;gt; ${FOO}&lt;/code&gt; in case &lt;code&gt;${FOO}&lt;/code&gt; has spaces in it.&lt;/small&gt;</description>
		<guid isPermaLink="false">post:ask.metafilter.com,2006:site.39266</guid>
		<pubDate>Wed, 31 May 2006 17:02:01 -0800</pubDate>
		<dc:creator>furtive</dc:creator>
		
			<category>shell</category>
		
			<category>bash</category>
		
			<category>korn</category>
		
			<category>ksh</category>
		
			<category>sh</category>
		
			<category>perl</category>
		
			<category>rhino</category>
		
			<category>python</category>
		
			<category>unix</category>
		
			<category>linux</category>
		
			<category>aix</category>
		
	</item> <item>
		<title>By: russilwvong</title>
		<link>http://ask.metafilter.com/39266/No-more-shell-games#606376</link>	
		<description>Have you read Kernighan and Pike&apos;s &lt;a href=&quot;http://www.amazon.com/exec/obidos/ASIN/013937681X/metafilter-20/ref=nosim/&quot;&gt;The UNIX Programming Environment&lt;/a&gt;?</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39266-606376</guid>
		<pubDate>Wed, 31 May 2006 17:11:36 -0800</pubDate>
		<dc:creator>russilwvong</dc:creator>
	</item><item>
		<title>By: furtive</title>
		<link>http://ask.metafilter.com/39266/No-more-shell-games#606381</link>	
		<description>No.  But it looks elite.  Thanks.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39266-606381</guid>
		<pubDate>Wed, 31 May 2006 17:20:55 -0800</pubDate>
		<dc:creator>furtive</dc:creator>
	</item><item>
		<title>By: NucleophilicAttack</title>
		<link>http://ask.metafilter.com/39266/No-more-shell-games#606421</link>	
		<description>set -o noclobber if you&apos;re in bash. We all fuck up from time to time. &lt;br&gt;
&lt;br&gt;
Unix Power Tools by O&apos;Reilly taught me a great deal.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39266-606421</guid>
		<pubDate>Wed, 31 May 2006 18:57:03 -0800</pubDate>
		<dc:creator>NucleophilicAttack</dc:creator>
	</item><item>
		<title>By: NucleophilicAttack</title>
		<link>http://ask.metafilter.com/39266/No-more-shell-games#606422</link>	
		<description>Oh right: alias rm=&quot;rm -i.&quot;  &lt;br&gt;
&lt;br&gt;
Touch a file called -i in /. &lt;br&gt;
&lt;br&gt;
That kind of thing.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39266-606422</guid>
		<pubDate>Wed, 31 May 2006 18:57:42 -0800</pubDate>
		<dc:creator>NucleophilicAttack</dc:creator>
	</item><item>
		<title>By: Rhomboid</title>
		<link>http://ask.metafilter.com/39266/No-more-shell-games#606439</link>	
		<description>If you&apos;re interested in writing portable scripts, then there&apos;s no better reference than &lt;a href=&quot;http://www.gnu.org/software/autoconf/manual/autoconf-2.57/html_chapter/autoconf_10.html#SEC114&quot;&gt;Chapter 10 of the autoconf manual&lt;/a&gt;.&lt;br&gt;
&lt;br&gt;
Some of my pet peeves:&lt;br&gt;
&lt;br&gt;
Always test your scripts with path and filenames that contain spaces.  You might not use them, but with operating systems like OS X becoming popular, many people are combining &quot;friendly&quot; filenames with unix shell concepts.&lt;br&gt;
&lt;br&gt;
This includes quoting arguments (e.g. cd &quot;$var&quot;) and always using the &quot;-print0 | xargs -0&quot; paradigm when processing the results of &apos;find&apos;.  In fact, you should almost always use this in favor of that awful &apos;-exec foo {}&apos; junk.  It can run multiple files per command, for example, which means your script will be faster on systems were fork/exec is slow.&lt;br&gt;
&lt;br&gt;
Though it is not as portable to ancient shells, $(foo) is much easier to code than `foo` because it allows for nested quoting with far fewer headaches.  Example:  echo &quot;$(echo &quot;one $(echo &quot;two $(echo &quot;three&quot;)&quot;)&quot;)&quot;&lt;br&gt;
&lt;br&gt;
Use functions instead of aliases.  They are much more powerful and a whole heck of a lot less confusing.  In fact, function use is a very good concept to use as much as possible, again unless you are trying to write very portable scripts (though it&apos;s only extremely ancient shells that don&apos;t support functions.)&lt;br&gt;
&lt;br&gt;
Learn the power of &quot;set -e&quot; for doing something that has multiple steps.  Also learn the power of &quot;set -x&quot; for debugging.  Along those lines, also consider &lt;a href=&quot;http://bashdb.sourceforge.net/&quot;&gt;bashdb&lt;/a&gt; for those hard to tackle bugs.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39266-606439</guid>
		<pubDate>Wed, 31 May 2006 19:38:56 -0800</pubDate>
		<dc:creator>Rhomboid</dc:creator>
	</item><item>
		<title>By: Rhomboid</title>
		<link>http://ask.metafilter.com/39266/No-more-shell-games#606448</link>	
		<description>And of course the normal stuff: &lt;a href=&quot;http://tiswww.tis.case.edu/~chet/bash/FAQ&quot;&gt;bash FAQ&lt;/a&gt;, &lt;a href=&quot;http://www.faqs.org/docs/abs/HTML/&quot;&gt;Advanced bash scripting guide&lt;/a&gt;.  Do be aware that not everyone&apos;s /bin/sh is bash, though, so if you use bash specific features set your shebang to /bin/bash not /bin/sh.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39266-606448</guid>
		<pubDate>Wed, 31 May 2006 19:46:31 -0800</pubDate>
		<dc:creator>Rhomboid</dc:creator>
	</item><item>
		<title>By: b1tr0t</title>
		<link>http://ask.metafilter.com/39266/No-more-shell-games#606515</link>	
		<description>1. I usually start scripts out in perl, unless I know that they will primarily involve calling other binaries&lt;br&gt;
2. If a perl script gets close to 1000 lines, I seriously consider reimplementing in a more maintainable language&lt;br&gt;
3. CVS, svn, or some other source code control system will save many headaches.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39266-606515</guid>
		<pubDate>Wed, 31 May 2006 20:46:52 -0800</pubDate>
		<dc:creator>b1tr0t</dc:creator>
	</item><item>
		<title>By: lundman</title>
		<link>http://ask.metafilter.com/39266/No-more-shell-games#606590</link>	
		<description>&lt;br&gt;
Good way to test is&lt;br&gt;
&lt;br&gt;
# if [ x&quot;${FOO}&quot; = x&quot;&quot; ]; then&lt;br&gt;
&lt;br&gt;
.. should handle FOO with spaces, or empty.&lt;br&gt;
&lt;br&gt;
Things I learnt recently that I wish someone had shown me a few years back, the &quot;set&quot; thing in sh.&lt;br&gt;
&lt;br&gt;
Like if you get:&lt;br&gt;
&quot;bit1 bit2 other items etc&quot; in $FOO, say, output from ls:&lt;br&gt;
&lt;br&gt;
# set -- $FOO&lt;br&gt;
# echo &quot;$3 and $5&quot;&lt;br&gt;
&lt;br&gt;
Would print &quot;other and etc&quot;.  Used to be always awk/cut mess.&lt;br&gt;
&lt;br&gt;
Ctrl-R in bash is beautiful, works like Ctrl-R in emacs.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39266-606590</guid>
		<pubDate>Wed, 31 May 2006 22:21:06 -0800</pubDate>
		<dc:creator>lundman</dc:creator>
	</item><item>
		<title>By: flabdablet</title>
		<link>http://ask.metafilter.com/39266/No-more-shell-games#606612</link>	
		<description>Using the shell&apos;s inbuilt string manipulation facilities instead of building little pipelines involving grep and/or sed can often make scripts run much more quickly.  Check the bash reference manual under &quot;parameter expansion&quot; to find out what kinds of stuff can be done.</description>
		<guid isPermaLink="false">comment:ask.metafilter.com,2006:site.39266-606612</guid>
		<pubDate>Wed, 31 May 2006 22:52:26 -0800</pubDate>
		<dc:creator>flabdablet</dc:creator>
	</item>
	</channel>
</rss>
