<?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>Erics Tech Blog &#187; bash</title>
	<atom:link href="http://eric.lubow.org/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://eric.lubow.org</link>
	<description>Thoughts, musings, and other idealistic (sometimes useful) systems and development hoopla.</description>
	<lastBuildDate>Fri, 18 Nov 2011 14:56:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Using Vi Mode Everywhere</title>
		<link>http://eric.lubow.org/2011/tips/using-vi-mode-everywhere/</link>
		<comments>http://eric.lubow.org/2011/tips/using-vi-mode-everywhere/#comments</comments>
		<pubDate>Tue, 15 Mar 2011 07:00:07 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=855</guid>
		<description><![CDATA[Not literally everywhere, but more places than usual. I have been looking for this solution for a long time and finally found it. Anyone who has ever worked around me knows that I do basically everything in Vi. Not only do I use it to edit files, but I use it as an IDE for [...]]]></description>
			<content:encoded><![CDATA[<p>Not literally everywhere, but more places than usual.  I have been looking for this solution for a long time and finally found it.  Anyone who has ever worked around me knows that I do basically everything in <a href="http://www.vim.org/">Vi</a>.<br />
<span id="more-855"></span><br />
Not only do I use it to edit files, but I use it as an IDE for development (even on my Mac instead of Textmate).  So the natural extension is for me to use it in the command prompt as well.  So in my <strong>.bashrc</strong> file, I have the line:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-o</span> <span style="color: #c20cb9; font-weight: bold;">vi</span></div></div>
<p>This allows me to navigate the bash console with the usual vim suspects: <em>h, j, k, l</em>.  In addition to that, I also get some fun ones like word movement <em>w</em> and the <em>dw</em> that goes along with it.</p>
<p>But the big winner for me is now I am able to use the vim environment and movement keys inside the irb (Ruby), Mongo, MySQL consoles (still not Redis though).  To do that, just add the following lines to the following files:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;bind -v&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> ~<span style="color: #000000; font-weight: bold;">/</span>.editrc<br />
$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;set editing-mode vi&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> ~<span style="color: #000000; font-weight: bold;">/</span>.inputrc</div></div>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2011/tips/using-vi-mode-everywhere/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Counting Frequencies of Frequencies</title>
		<link>http://eric.lubow.org/2010/tips/counting-frequencies-of-frequencies/</link>
		<comments>http://eric.lubow.org/2010/tips/counting-frequencies-of-frequencies/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 12:30:13 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=775</guid>
		<description><![CDATA[Lots of people forget about the usefulness of the core utilities (the tools available in Bash). I am even pretty guilty of it at times with such quick and easy things like Perl, Ruby, or Python that allow you to process items from the command line. However, they load up an entire interpreter. It is [...]]]></description>
			<content:encoded><![CDATA[<p>Lots of people forget about the usefulness of the core utilities (the tools available in Bash).  I am even pretty guilty of it at times with such quick and easy things like Perl, Ruby, or Python that allow you to process items from the command line.  However, they load up an entire interpreter.  It is usually better to use the coreutils.<br />
<span id="more-775"></span></p>
<p>I&#8217;ll give you the specific example I had to deal with, but this can be extrapolated out and I&#8217;m sure reused for other purposes.  I have lists of email addresses of who has received emails that were sent over the past 7 days.  I do this with a find command since the address files are created nightly.  They are laid out with 1 email address (and some other meta data) per line.  You&#8217;ll notice the <strong>awk</strong> in the find command; that&#8217;s just to extract the email address from the line to make things easier to work with.</p>
<p>The overall goal here is find out how many times people were emailed over the past 7 days.  Another way of saying it is, how many people received 1,2,3,4, etc mailings over the past week.  This is an exercise in aggregation.</p>
<p>First, here is everything in its entirety and then I will proceed to go through it all piece by piece.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #000000; font-weight: bold;">/</span>nfs<span style="color: #000000; font-weight: bold;">/</span>mailings<span style="color: #000000; font-weight: bold;">/</span>lists<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-maxdepth</span> <span style="color: #000000;">3</span> <span style="color: #660033;">-ctime</span> <span style="color: #660033;">-7</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;master_list.txt&quot;</span> \<br />
<span style="color: #660033;">-exec</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $2}'</span> <span style="color: #ff0000;">'{}'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>emails.txt \; <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>emails.txt <span style="color: #000000; font-weight: bold;">|</span> \<br />
<span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{ for (i=1;i&lt; =NF;i++) count[$i]++ } &nbsp;END { for (i in count) print count[i], i }'</span> <span style="color: #007800;">$*</span> <span style="color: #000000; font-weight: bold;">|</span>\<br />
<span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $1}'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{ for (i=1;i&lt;=NF;i++) count[$i]++ } &nbsp;\<br />
END { for (i in count) print count[i], i }'</span> <span style="color: #007800;">$*</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $2,$1}'</span> <span style="color: #000000; font-weight: bold;">|</span> \<br />
<span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-n</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>emails.txt<br />
<span style="color: #000000;">1</span> <span style="color: #000000;">93278</span><br />
<span style="color: #000000;">2</span> <span style="color: #000000;">21415</span><br />
<span style="color: #000000;">3</span> <span style="color: #000000;">16924</span><br />
<span style="color: #000000;">4</span> <span style="color: #000000;">3064131</span><br />
<span style="color: #000000;">5</span> <span style="color: #000000;">548421</span><br />
<span style="color: #000000;">6</span> <span style="color: #000000;">102</span></div></div>
<p>A quick breakdown goes as follows.</p>
<p>1) Find all mailing list master files (-name) that aren&#8217;t archived (-maxdepth) that were created in the last 7 days (-ctime) and print their id (exec awk) to a tmp file. Everything here needs to be concatenated so as not to clobber the emails added by the previous day.<br />
</code></p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #000000; font-weight: bold;">/</span>nfs<span style="color: #000000; font-weight: bold;">/</span>mailings<span style="color: #000000; font-weight: bold;">/</span>lists<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-maxdepth</span> <span style="color: #000000;">3</span> <span style="color: #660033;">-ctime</span> <span style="color: #660033;">-7</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;master_list.txt&quot;</span>\<br />
<span style="color: #660033;">-exec</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $2}'</span> <span style="color: #ff0000;">'{}'</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>emails.txt \;</div></div>
<p>2) The next piece word frequency counts the emails and prints them to STDOUT in a <cnt> <email> manner. This is to say that the first column is a count of how many times the second column has received an email over the past 7 days.  Now we know how many emails each individual has received and we need to aggregate again.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>emails.txt <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{ for (i=1;i&lt; =NF;i++) count[$i]++ } &nbsp;\<br />
END { for (i in count) print count[i], i }'</span> <span style="color: #007800;">$*</span></div></div>
<p>3) Because I am using pipes, instead of printing the previous step to STDOUT, I only print the first column (count) and then count the frequency of that.<br />
</code></p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $1} | awk '</span><span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">i</span>=<span style="color: #000000;">1</span>;i<span style="color: #000000; font-weight: bold;">&lt;</span> =NF;i++<span style="color: #7a0874; font-weight: bold;">&#41;</span> count<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #007800;">$i</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>++ <span style="color: #7a0874; font-weight: bold;">&#125;</span> \<br />
END <span style="color: #7a0874; font-weight: bold;">&#123;</span> <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>i <span style="color: #000000; font-weight: bold;">in</span> count<span style="color: #7a0874; font-weight: bold;">&#41;</span> print count<span style="color: #7a0874; font-weight: bold;">&#91;</span>i<span style="color: #7a0874; font-weight: bold;">&#93;</span>, i <span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #ff0000;">' $*</span></div></div>
<p>4) Then I swap the columns so I get <freq>
<person_count>.  This is much more logical and is the way we generally read tables.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $2,$1}'</span></div></div>
<p>5) Finally I sort that result numerically ascending and remove the temp file so I don't cat things to it again.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-n</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>emails.txt</div></div>
<p>6) We get our output in an easy to read format (Note: I added the headers).</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Freq &nbsp; Total<br />
&nbsp;<span style="color: #000000;">1</span> &nbsp; &nbsp; <span style="color: #000000;">93278</span><br />
&nbsp;<span style="color: #000000;">2</span> &nbsp; &nbsp; <span style="color: #000000;">21415</span><br />
&nbsp;<span style="color: #000000;">3</span> &nbsp; &nbsp; <span style="color: #000000;">16924</span><br />
&nbsp;<span style="color: #000000;">4</span> &nbsp; &nbsp; <span style="color: #000000;">3064131</span><br />
&nbsp;<span style="color: #000000;">5</span> &nbsp; &nbsp; <span style="color: #000000;">548421</span><br />
&nbsp;<span style="color: #000000;">6</span> &nbsp; &nbsp; <span style="color: #000000;">102</span></div></div>
</person_count></freq></code></email></cnt></p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/databases/mysql/counting-email-addresses-by-domain-in-mysql/' rel='bookmark' title='Counting Email Addresses By Domain in MySQL'>Counting Email Addresses By Domain in MySQL</a></li>
<li><a href='http://eric.lubow.org/2007/perl/emailfind/' rel='bookmark' title='Email::Find'>Email::Find</a></li>
<li><a href='http://eric.lubow.org/2008/linux-security/checking-for-a-dos/' rel='bookmark' title='Checking For A DoS'>Checking For A DoS</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/tips/counting-frequencies-of-frequencies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple Input Locations From Bash Into Ruby</title>
		<link>http://eric.lubow.org/2010/ruby/multiple-input-locations-from-bash-into-ruby/</link>
		<comments>http://eric.lubow.org/2010/ruby/multiple-input-locations-from-bash-into-ruby/#comments</comments>
		<pubDate>Thu, 13 May 2010 12:00:59 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=723</guid>
		<description><![CDATA[I have been trying to figure out how, while using OptionParser to be able to check for files being input on the command line and if they don&#8217;t exist, check other input streams (like Bash). This initially wasn&#8217;t very easy since input streams are blocking. So with a little help from friends (thanks roberto), I [...]]]></description>
			<content:encoded><![CDATA[<p>I have been trying to figure out how, while using <a href="http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html">OptionParser</a> to be able to check for files being input on the command line and if they don&#8217;t exist, check other input streams (like Bash).  This initially wasn&#8217;t very easy since input streams are blocking.  So with a little help from friends (thanks roberto), I was able to use his method of non-blocking IO and wrap it in a begin/rescue block.  I also took a little advice given in this <a href="http://www.stackoverflow.com">Stack Overflow</a> question called <a href="http://stackoverflow.com/questions/273262/best-practices-with-stdin-in-ruby">Best Practices with STDIN in Ruby.</a><br />
<span id="more-723"></span><br />
First we need to get the file list off the command line and assume that anything left are files.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#0066ff; font-weight:bold;">@opts</span> = OptionParser.<span style="color:#9900CC;">new</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>o<span style="color:#006600; font-weight:bold;">|</span><br />
...<br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#0066ff; font-weight:bold;">@opts</span>.<span style="color:#9900CC;">parse</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>args<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#0066ff; font-weight:bold;">@files</span> = args</div></div>
<p>Since we are using threads, I open up a thread for STDIN and killed it when we run out of input.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'fcntl'</span><br />
threads = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span><br />
<br />
<span style="color:#008000; font-style:italic;"># Set $stdin to be non-blocking</span><br />
<span style="color:#ff6633; font-weight:bold;">$stdin</span>.<span style="color:#9900CC;">fcntl</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Fcntl::F_SETFL</span>,<span style="color:#6666ff; font-weight:bold;">Fcntl::O_NONBLOCK</span><span style="color:#006600; font-weight:bold;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
<br />
threads<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">begin</span><br />
&nbsp; &nbsp; <span style="color:#ff6633; font-weight:bold;">$stdin</span>.<span style="color:#9900CC;">each_line</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span> &nbsp;<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;STDIN: #{line}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Errno</span>::EAGAIN<br />
&nbsp; &nbsp; threads.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># Remove this thread since we won't be reading from $stdin</span><br />
&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">run</span></div></div>
<p>Now its time for the files.</p>
<div class="codecolorer-container ruby default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#0066ff; font-weight:bold;">@files</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span><br />
&nbsp; threads.<span style="color:#9900CC;">push</span> <span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># do stuff with 'file'</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#008000; font-style:italic;"># Put it all together and have the threads run</span><br />
threads.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>thread<span style="color:#006600; font-weight:bold;">|</span> &nbsp;thread.<span style="color:#9900CC;">join</span> <span style="color:#006600; font-weight:bold;">&#125;</span></div></div>
<p>Using these code snippets, you will be able to use input from both files on the command line and STDIN:</p>
<ol>
<li>$ myscript.rb file1 file2</li>
<li>$ cat foo | myscript.rb file1 file2</li>
<li>$ myscript.rb file1 file2 &lt; foo</li>
</ol>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/ruby/testing-ruby/should-i-mock-kernel-exit/' rel='bookmark' title='Should I Mock Kernel#exit'>Should I Mock Kernel#exit</a></li>
<li><a href='http://eric.lubow.org/2009/ruby/parsing-ini-files-with-ruby/' rel='bookmark' title='Parsing Ini Files With Ruby'>Parsing Ini Files With Ruby</a></li>
<li><a href='http://eric.lubow.org/2009/system-administration/git-branch-name-in-your-bash-prompt/' rel='bookmark' title='Git Branch Name in Your Bash Prompt'>Git Branch Name in Your Bash Prompt</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/ruby/multiple-input-locations-from-bash-into-ruby/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Count Instead of Sequence</title>
		<link>http://eric.lubow.org/2010/mac/count-instead-of-sequence/</link>
		<comments>http://eric.lubow.org/2010/mac/count-instead-of-sequence/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 10:30:29 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=629</guid>
		<description><![CDATA[I use Bash one liners a lot. I think they are an important part of any programmers and sysadmins toolkit. If you can&#8217;t write a bash one liner, even a simple iterator, then you really need to learn. I promise it will make your life infinitely more pleasant. Frequently I find myself writing things that [...]]]></description>
			<content:encoded><![CDATA[<p>I use Bash one liners a lot.  I think they are an important part of any programmers and sysadmins toolkit.  If you can&#8217;t write a bash one liner, even a simple iterator, then you really need to learn.  I promise it will make your life infinitely more pleasant.</p>
<p>Frequently I find myself writing things that require a loop or an increment of numbers.  A good example would be like something that would walk over my web servers and check their uptime, load averages, etc.  Using <strong>seq</strong>, that&#8217;s easy.  But since Mac OS X doesn&#8217;t come with the <strong>seq</strong> command, I would previously improvise.<br />
<span id="more-629"></span></p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># Linux system</span><br />
$ <span style="color: #000000; font-weight: bold;">for</span> n <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">seq</span> <span style="color: #000000;">1</span> <span style="color: #000000;">6</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;web<span style="color: #007800;">${n}</span>: &quot;</span>; <span style="color: #c20cb9; font-weight: bold;">ssh</span> web<span style="color: #007800;">$n</span> <span style="color: #c20cb9; font-weight: bold;">uptime</span>; <span style="color: #000000; font-weight: bold;">done</span>;<br />
web1: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">50</span>, &nbsp;<span style="color: #000000;">4</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.03</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web2: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">48</span>, &nbsp;<span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.07</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web3: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">47</span>, &nbsp;<span style="color: #000000;">3</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.04</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web4: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">47</span>, &nbsp;<span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.03</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web5: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">45</span>, &nbsp;<span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.01</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web6: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">45</span>, &nbsp;<span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.01</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Mac</span><br />
$ <span style="color: #000000; font-weight: bold;">for</span> n <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;1 2 3 4 5 6&quot;</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;web<span style="color: #007800;">${n}</span>&quot;</span>; <span style="color: #c20cb9; font-weight: bold;">ssh</span> web<span style="color: #007800;">$n</span> <span style="color: #c20cb9; font-weight: bold;">uptime</span>; <span style="color: #000000; font-weight: bold;">done</span>;<br />
web1: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">50</span>, &nbsp;<span style="color: #000000;">4</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.03</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web2: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">48</span>, &nbsp;<span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.07</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web3: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">47</span>, &nbsp;<span style="color: #000000;">3</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.04</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web4: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">47</span>, &nbsp;<span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.03</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web5: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">45</span>, &nbsp;<span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.01</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web6: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">45</span>, &nbsp;<span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.01</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span></div></div>
<p>That is no longer the case.  I could have written this myself, but since I am not for reinventing the wheel, I Google&#8217;d around and found <a href="http://www.askdavetaylor.com/step_through_count_numeric_values_bash_shell_script.htm">count</a>.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #000000; font-weight: bold;">for</span> n <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span>count <span style="color: #000000;">1</span> <span style="color: #000000;">6</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;web<span style="color: #007800;">${n}</span>&quot;</span>; <span style="color: #c20cb9; font-weight: bold;">ssh</span> web<span style="color: #007800;">$n</span> <span style="color: #c20cb9; font-weight: bold;">uptime</span>; <span style="color: #000000; font-weight: bold;">done</span>;<br />
web1: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">50</span>, &nbsp;<span style="color: #000000;">4</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.03</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web2: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">48</span>, &nbsp;<span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.07</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web3: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">47</span>, &nbsp;<span style="color: #000000;">3</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.04</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web4: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">47</span>, &nbsp;<span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.03</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web5: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">45</span>, &nbsp;<span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.01</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span><br />
web6: <span style="color: #000000;">18</span>:<span style="color: #000000;">36</span>:<span style="color: #000000;">56</span> up <span style="color: #000000;">82</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">45</span>, &nbsp;<span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>, &nbsp;load average: <span style="color: #000000;">0.01</span>, <span style="color: #000000;">0.00</span>, <span style="color: #000000;">0.00</span></div></div>
<p>I can&#8217;t take any credit for this script, nor do I plan to.  But I feel that all Mac shell users should have this available to them.  The original was written by <a href="http://www.askdavetaylor.com/">Dave Taylor</a> and is located <a href="http://www.askdavetaylor.com/step_through_count_numeric_values_bash_shell_script.html">here</a>. I strongly suggest that in addition to copying the script, you read his explanation of its utility. It give you the ability to increment or decrement, whichever you find the need for.</p>
<p>To install, just copy and paste into <strong>/usr/local/bin/count</strong> and then <strong>chmod +x /usr/local/bin/count</strong>.</p>
<p>Here is the script in its entirety.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:450px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/bin/sh</span><br />
<br />
<span style="color: #666666; font-style: italic;"># count - step through numeric values until you get to the max value</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$#&quot;</span> <span style="color: #660033;">-lt</span> <span style="color: #000000;">2</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Usage: count {increment}&quot;</span><br />
&nbsp; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
<span style="color: #007800;">counter</span>=<span style="color: #ff0000;">&quot;$1&quot;</span><br />
<span style="color: #007800;">max</span>=<span style="color: #ff0000;">&quot;$2&quot;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$#&quot;</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">2</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; <span style="color: #007800;">step</span>=<span style="color: #000000;">1</span><br />
<span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; <span style="color: #007800;">step</span>=<span style="color: #007800;">$3</span><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$counter</span> <span style="color: #660033;">-le</span> <span style="color: #007800;">$max</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">do</span><br />
&nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$counter</span><br />
&nbsp; <span style="color: #007800;">counter</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$counter</span> + <span style="color: #007800;">$step</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">done</span><br />
<br />
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span></div></div>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2010/mac/capistrano-hangs-on-mac-os-x-leopard/' rel='bookmark' title='Capistrano Hangs on Mac OS X Leopard'>Capistrano Hangs on Mac OS X Leopard</a></li>
<li><a href='http://eric.lubow.org/2010/tips/counting-frequencies-of-frequencies/' rel='bookmark' title='Counting Frequencies of Frequencies'>Counting Frequencies of Frequencies</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2010/mac/count-instead-of-sequence/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Git Branch Name in Your Bash Prompt</title>
		<link>http://eric.lubow.org/2009/system-administration/git-branch-name-in-your-bash-prompt/</link>
		<comments>http://eric.lubow.org/2009/system-administration/git-branch-name-in-your-bash-prompt/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 11:50:32 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=461</guid>
		<description><![CDATA[I work with a few repositories at any given time. And during that time, I typically have multiple branches created for each repository. I figured that it would make my life easier if I knew which branch and/or repository I was working in. Luckily, very little hackery is required here since the git distribution already [...]]]></description>
			<content:encoded><![CDATA[<p>I work with a few repositories at any given time. And during that time, I typically have multiple branches created for each repository.  I figured that it would make my life easier if I knew which branch and/or repository I was working in.  Luckily, very little hackery is required here since the git distribution already comes with such a tool.  (Note: If you didn&#8217;t build Git from source, then you may not have this file.)<br />
<span id="more-461"></span><br />
First thing I did was copy the git auto-completion file into my home directory:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">elubow<span style="color: #000000; font-weight: bold;">@</span>beacon repo$ <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>git<span style="color: #000000; font-weight: bold;">/</span>contrib<span style="color: #000000; font-weight: bold;">/</span>git-completion.bash ~<span style="color: #000000; font-weight: bold;">/</span>.git-completion.sh</div></div>
<p>Then I edited my .bashrc file to load up my new <strong>PS1</strong> variable (PS1 is variable name for the Bash prompt) with git information only if we are currently in a repository.  Don&#8217;t forget to set a default <strong>PS1</strong> in case your .bashrc can&#8217;t find your <strong>.git-completion.sh</strong> file.  Another item of note is that you should have the if state below load after your load your <strong>PATH</strong> variable.  This removes the potential for <strong>git</strong> having to be located after every commend.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># Set the base PS1</span><br />
<span style="color: #007800;">PS1</span>=<span style="color: #ff0000;">&quot;\u:\h \W$ &quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Source the git bash completion file</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> ~<span style="color: #000000; font-weight: bold;">/</span>.git-completion.sh <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">source</span> ~<span style="color: #000000; font-weight: bold;">/</span>.git-completion.sh<br />
&nbsp; &nbsp; <span style="color: #007800;">GIT_PS1_SHOWDIRTYSTATE</span>=<span style="color: #c20cb9; font-weight: bold;">true</span><br />
&nbsp; &nbsp; <span style="color: #007800;">PS1</span>=<span style="color: #ff0000;">'\u@\h$(__git_ps1 &quot; (%s)&quot;) \W$ '</span><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
<span style="color: #7a0874; font-weight: bold;">export</span> PS1</div></div>
<p>You&#8217;ll notice I also set a variable called <strong>GIT_PS1_SHOWDIRTYSTATE</strong>.  What this variable does is it adds a <strong>*</strong> to the branch name if the branch has been changed.</p>
<p>There are other options here if you are looking for something more in depth.  <a href="http://volnitsky.com/project/git-prompt/">Git Prompt</a> provides you with a much more in depth version on the command line fo your dirty files and is much more visually oriented.</p>
<p>Additionally, you now get to make use of the other auto-complete features that this script was actually intended for.</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">elubow<span style="color: #000000; font-weight: bold;">@</span>beacon <span style="color: #7a0874; font-weight: bold;">&#40;</span>master<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> repo$ <span style="color: #c20cb9; font-weight: bold;">git</span> sta<br />
stage &nbsp; &nbsp;stash &nbsp; &nbsp;status</div></div>
<p>Note the difference between my prompt at the beginning of the post and the end.  When I am not in a repository, nothing shows in my <strong>PS1</strong> variable.</p>
<p>It&#8217;s just that simple to do this and definitely saves some time. Enjoy.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/misc/converting-from-subversion-to-git/' rel='bookmark' title='Converting From Subversion To Git'>Converting From Subversion To Git</a></li>
<li><a href='http://eric.lubow.org/2009/misc/printing-new-lines-in-bash/' rel='bookmark' title='Printing New Lines in Bash'>Printing New Lines in Bash</a></li>
<li><a href='http://eric.lubow.org/2009/tips/git-command-aliases/' rel='bookmark' title='Git Command Aliases'>Git Command Aliases</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/system-administration/git-branch-name-in-your-bash-prompt/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Printing New Lines in Bash</title>
		<link>http://eric.lubow.org/2009/misc/printing-new-lines-in-bash/</link>
		<comments>http://eric.lubow.org/2009/misc/printing-new-lines-in-bash/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 15:00:55 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=211</guid>
		<description><![CDATA[Ran across this the other day and decided it required sharing. If you want to print a new line &#8216;\n&#8216; in an echo statement in bash, one would think its just as simple as: beacon:~ elubow$ echo &#34;This is a test\n&#34; This is a test\n The problem is that this doesn&#8217;t interpolate the newline character. [...]]]></description>
			<content:encoded><![CDATA[<p>Ran across this the other day and decided it required sharing.  If you want to print a new line &#8216;<strong>\n</strong>&#8216; in an <strong>echo</strong> statement in bash, one would think its just as simple as:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">beacon:~ elubow$ echo &quot;This is a test\n&quot;<br />
This is a test\n</div></div>
<p>The problem is that this doesn&#8217;t interpolate the newline character.  (For more information on interpolation, see Wikipedia <a href="http://en.wikipedia.org/wiki/Variable_interpolation#Interpolation">here</a>.)  In order to have the newline interpolated, you need to add the command line switch &#8216;<strong>-e</strong>&#8216;.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">beacon:~ elubow$ echo -e &quot;This is a test\n&quot;<br />
This is a test</div></div>
<p>This will force Bash to interpolate any non-literal characters in the quotes.  <strong>Note:</strong> Unlike Perl, single or double quotes don&#8217;t matter here when Bash is deciding whether or not to interpolate the new line characters.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/system-administration/git-branch-name-in-your-bash-prompt/' rel='bookmark' title='Git Branch Name in Your Bash Prompt'>Git Branch Name in Your Bash Prompt</a></li>
<li><a href='http://eric.lubow.org/2010/ruby/multiple-input-locations-from-bash-into-ruby/' rel='bookmark' title='Multiple Input Locations From Bash Into Ruby'>Multiple Input Locations From Bash Into Ruby</a></li>
<li><a href='http://eric.lubow.org/2009/misc/converting-from-subversion-to-git/' rel='bookmark' title='Converting From Subversion To Git'>Converting From Subversion To Git</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/misc/printing-new-lines-in-bash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick Date Calculations With Date</title>
		<link>http://eric.lubow.org/2009/system-administration/quick-date-calculations-with-date/</link>
		<comments>http://eric.lubow.org/2009/system-administration/quick-date-calculations-with-date/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 14:00:29 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[bash]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/?p=239</guid>
		<description><![CDATA[I frequently find myself needing to do quick date calculations in order to make scripts run when I want them to or how I want them to. Usually Date::Calc is just a bit too heavily, especially if it&#8217;s something as simple as a BASH script. As it happens, date is quite a powerful tool for [...]]]></description>
			<content:encoded><![CDATA[<p>I frequently find myself needing to do quick date calculations in order to make scripts run when I want them to or how I want them to.  Usually <a href="http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod">Date::Calc</a> is just a bit too heavily, especially if it&#8217;s something as simple as a BASH script.  As it happens, <strong>date</strong> is quite a powerful tool for some command line fu.</p>
<p>For example, to find the first and last day of last month:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007800;">FIRST_DOLM</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;-1 month -<span style="color: #007800;">$(($(date +%d)</span>-1)) days&quot;</span> <span style="color: #ff0000;">&quot;+%Y-%m-%d&quot;</span><span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #007800;">LAST_DOLM</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;-<span style="color: #007800;">$(date +%d)</span> days&quot;</span> <span style="color: #ff0000;">&quot;+%Y-%m-%d&quot;</span><span style="color: #000000; font-weight: bold;">`</span></div></div>
<p>You can do the same thing using the <strong>date</strong> command on a mac with a slightly different set of switches:</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #007800;">FIRST_DOLM</span>=<span style="color: #000000; font-weight: bold;">`/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #660033;">-v1d</span> <span style="color: #660033;">-v-1m</span> <span style="color: #ff0000;">&quot;+%Y-%m-%d&quot;</span><span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #007800;">LAST_DOLM</span>=<span style="color: #000000; font-weight: bold;">`/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #660033;">-v31d</span> <span style="color: #660033;">-v-1m</span> <span style="color: #ff0000;">&quot;+%Y-%m-%d&quot;</span><span style="color: #000000; font-weight: bold;">`</span></div></div>
<p>Both of these will produce the same results:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">2009-06-01<br />
2009-06-30</div></div>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2009/system-administration/quick-date-calculations-with-date/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Deleting Lots Of Files (SysAdmin problem solving exercise)</title>
		<link>http://eric.lubow.org/2007/system-administration/deleting-lots-of-files-sysadmin-problem-solving-exercise/</link>
		<comments>http://eric.lubow.org/2007/system-administration/deleting-lots-of-files-sysadmin-problem-solving-exercise/#comments</comments>
		<pubDate>Mon, 17 Dec 2007 11:15:12 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[logrotate]]></category>

		<guid isPermaLink="false">http://eric.lubow.org/blog/2007/system-administration/45/deleting-lots-of-files-sysadmin-problem-solving-exercise/</guid>
		<description><![CDATA[Since I know I am not the first (or the last) to make a typo in logrotate and not catch it for a while&#8230;someone else must have been in the position of having to delete a lot of files in the same manner. I recently learned that, as usual, there is more than one way [...]]]></description>
			<content:encoded><![CDATA[<p>Since I know I am not the first (or the last) to make a typo in logrotate and not catch it for a while&#8230;someone else must have been in the position of having to delete a lot of files in the same manner.  I recently learned that, as usual, there is more than one way to handle it.</p>
<p>To put the situation in context, I basically allowed thousands of <em>mail.*</em> files to be created.  These files littered the <em>/var/log/</em> directory and basically slowed down the entire file system access.  I figured out this a number of ways.</p>
<p>The first way was when I tried to do an <strong>ls</strong> anywhere, it would just hang.  My first reaction was to check to see what was eating up the CPU.  To do this, I did a <strong>top</strong>. I noticed that <strong>logrotate</strong> was hogging all the CPU cycles.  Since I know that <strong>logrotate</strong> basically only operates on one parent directory (by default) <em>/var/log</em>, I headed on over there and did an <strong>ls</strong>.  Once again, it just hung.  Then I figured the file system was slow and decided to check out some file system information.  The next two commands I ran were <strong>df -h</strong> and <strong>df -i</strong>. I ran the <strong>df -h</strong> to see if we were out of disk space (and yes I lazily use human readable format).  I ran the second to check to see how many inodes were in use. (For more information on inodes, check out the <a href="http://en.wikipedia.org/">wikipedia</a> entry <a href="http://en.wikipedia.org/wiki/Inode">here</a>).</p>
<p>Now that I know the system is short on inodes, I checked out the output of <strong>lsof</strong>.  Now I know that we have some serious problems in the <em>/var/log</em> dir.  After some quick investigation, I realized that there were too many <em>mail.*</em> files.  How do I get rid of them? Glad you asked&#8230; Let&#8217;s assume that we want to delete ALL the <em>mail.*</em> files in the <em>/var/log</em> directory.</p>
<p>1) The easiest way is to do it with <strong>find</strong>:<br />
1a) Using <strong>find</strong>&#8216;s delete command:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[root@eric] /var/log # find ./ -type f -name &quot;mail.*&quot; -delete</div></div>
<p>or<br />
1b) using <strong>find</strong>&#8216;s exec command with <strong>rm</strong>:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[root@eric] /var/log # find ./ -type f -name &quot;mail.*&quot; -exec rm -rf '{}' \;</div></div>
<p>These will work, but either will be slow since they doesn&#8217;t do batch execution.</p>
<p>2) A slightly more preferred way is to use bash:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[root@eric] /var/log # for n in mail.*; do rm -v $n; done;</div></div>
<p>This is a little faster, but will still be relatively slow since there is no batch execution. (Note: The -v in the rm will cause quite a bit of output since it is showing you EVERY file it deletes.  Feel free to leave this out if you really screwed up.)</p>
<p>3) The actual preferred method is to use <strong>find</strong>:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[root@eric] /var/log # find ./ -type f -name &quot;mail.*&quot; | xargs rm -f</div></div>
<p>I believe this is the preferred method because although it removes the files one at a time, it is more efficient for the file system since it batches it up.</p>
<p>There are certainly other ways to accomplish this task.  It can always be done with a Perl one-liner or even using some Perl modules to save some time.  These are just a few ideas to point someone in the right direction.</p>


<p>Related posts:<ol><li><a href='http://eric.lubow.org/2009/ruby/parsing-ini-files-with-ruby/' rel='bookmark' title='Parsing Ini Files With Ruby'>Parsing Ini Files With Ruby</a></li>
<li><a href='http://eric.lubow.org/2007/linux-security/ssh-organization-tips/' rel='bookmark' title='SSH Organization Tips'>SSH Organization Tips</a></li>
<li><a href='http://eric.lubow.org/2007/perl/filefind/' rel='bookmark' title='File::Find'>File::Find</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://eric.lubow.org/2007/system-administration/deleting-lots-of-files-sysadmin-problem-solving-exercise/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

