<?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>thirstymind.org &#187; macports</title>
	<atom:link href="http://www.thirstymind.org/tag/macports/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thirstymind.org</link>
	<description>andrew watts' weblog</description>
	<lastBuildDate>Sat, 17 Oct 2009 22:11:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Handling whitespace in bash for loops</title>
		<link>http://www.thirstymind.org/2008/06/27/handling-whitespace-in-bash-for-loops/</link>
		<comments>http://www.thirstymind.org/2008/06/27/handling-whitespace-in-bash-for-loops/#comments</comments>
		<pubDate>Sat, 28 Jun 2008 03:31:24 +0000</pubDate>
		<dc:creator>Andrew Watts</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[macports]]></category>

		<guid isPermaLink="false">http://www.thirstymind.org/?p=366</guid>
		<description><![CDATA[Tonight I was trying to cleanup my macports installation by uninstalling the packages that are no longer active.  I decided to write a script to do this, instead of manually uninstalling them one by one.  But in writing the script I came across something I had never seen and the google didn&#8217;t give [...]]]></description>
			<content:encoded><![CDATA[<p>Tonight I was trying to cleanup my <a href="http://www.macports.org/">macports</a> installation by uninstalling the packages that are no longer active.  I decided to write a script to do this, instead of manually uninstalling them one by one.  But in writing the script I came across something I had never seen and the google didn&#8217;t give me a clear answer so I thought I would share.</p>
<p>A typical for loop might look like:</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1">$ <span class="kw1">for</span> <span class="kw2">file</span> <span class="kw1">in</span> `<span class="kw2">ls</span>`</div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> <span class="kw1">do</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> &nbsp; &nbsp; <span class="kw3">echo</span> <span class="re1">$file</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> <span class="kw1">done</span></div>
</li>
</ol>
</div>
<p>And this assumes there are no spaces in any of the files returned by ls.  I did not know this.  So I started doing something similar with this:</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1">$ <span class="kw1">for</span> package <span class="kw1">in</span> `port installed <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;NF==2{print $0}&#39;</span> `</div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> <span class="kw1">do</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> &nbsp; &nbsp; <span class="kw3">echo</span> <span class="re1">$package</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> <span class="kw1">done</span></div>
</li>
</ol>
</div>
<p>But I got output that looked like this:</p>
<div class="geshi no none">
<ol>
<li class="li1">
<div class="de1">curl
</div>
</li>
<li class="li1">
<div class="de1">@7.17.1_0
</div>
</li>
<li class="li1">
<div class="de1">gettext
</div>
</li>
<li class="li1">
<div class="de1">@0.17_2</div>
</li>
</ol>
</div>
<p>And wanted output like this:</p>
<div class="geshi no none">
<ol>
<li class="li1">
<div class="de1">curl @7.17.1_0
</div>
</li>
<li class="li1">
<div class="de1">gettext @0.17_2</div>
</li>
</ol>
</div>
<p>The problem is the awk output had spaces in it which were split by the for loop processing.  And well to handle the spaces as desired you need to pipe the output to a while loop like so:</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1">$ port installed <span class="sy0">|</span> <span class="kw2">awk</span> <span class="st0">&#39;NF==2{print $0}&#39;</span> <span class="sy0">|</span> <span class="kw1">while</span> <span class="kw2">read</span> pakage</div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> <span class="kw1">do</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> &nbsp; &nbsp; <span class="kw2">sudo</span> port uninstall <span class="re1">$package</span></div>
</li>
<li class="li1">
<div class="de1"><span class="sy0">&gt;</span> <span class="kw1">done</span></div>
</li>
</ol>
</div>
<p>And now $package is what I expect including the spaces, and my macports installation is a little cleaner.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thirstymind.org/2008/06/27/handling-whitespace-in-bash-for-loops/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

