<?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>tail -f findings.out &#187; CLI</title>
	<atom:link href="http://dancingpenguinsoflight.com/category/cli/feed/" rel="self" type="application/rss+xml" />
	<link>http://dancingpenguinsoflight.com</link>
	<description></description>
	<lastBuildDate>Sun, 21 Aug 2011 14:33:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Bag of useful Bash aliases and functions</title>
		<link>http://dancingpenguinsoflight.com/2011/05/bag-of-useful-bash-aliases-and-functions/</link>
		<comments>http://dancingpenguinsoflight.com/2011/05/bag-of-useful-bash-aliases-and-functions/#comments</comments>
		<pubDate>Sun, 15 May 2011 01:10:23 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[CLI]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1968</guid>
		<description><![CDATA[Here are a number of assorted aliases and functions for Bash that I&#8217;ve made in the last few months and found to be useful. Enjoy! Sysadminish These two print the current value of the swappiness parameter and tail recent syslog &#8230; <a href="http://dancingpenguinsoflight.com/2011/05/bag-of-useful-bash-aliases-and-functions/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here are a number of assorted aliases and functions for Bash that I&#8217;ve made in the last few months and found to be useful. Enjoy!<br />
<span id="more-1968"></span></p>
<h2>Sysadminish</h2>
<p>These two print the current value of the <a title="Article on kernel swappiness" href="http://www.linuxvox.com/2009/10/what-is-the-linux-kernel-parameter-vm-swappiness/" target="_blank">swappiness parameter</a> and tail recent syslog messages:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><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;">alias</span> <span style="color: #007800;">swappy</span>=<span style="color: #ff0000;">&quot;sudo /sbin/sysctl vm.swappiness&quot;</span><br />
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">sysl</span>=<span style="color: #ff0000;">&quot;sudo tail /var/log/syslog&quot;</span></div></td></tr></tbody></table></div>
<p>This converts unix timestamps (e.g. 1305401097) that you might find in logfiles to a more easily readable date format:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><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;">function</span> fromunixtime <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;require 'ctime.pl'; print &amp;ctime($1);&quot;</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>
<p>The first is simply a quick way to check internet connectivity. Next are some super short ping aliases to avoid Ctrl+c all the time when you just need to verify that something responds. Pass them a URL to get one or three ping attempts:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><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;">alias</span> <span style="color: #007800;">pingoo</span>=<span style="color: #ff0000;">&quot;ping -c 3 www.google.com&quot;</span><br />
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">p1</span>=<span style="color: #ff0000;">&quot;ping -c 1 &quot;</span><br />
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">p3</span>=<span style="color: #ff0000;">&quot;ping -c 3 &quot;</span></div></td></tr></tbody></table></div>
<p>Pass a webpage URL and it will download the source to a file named after the domain (if you&#8217;re passing a root page, e.g. http://www.cnn.com/) or the page name (e.g. contact.html):</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><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;">function</span> getsrc <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #007800;">$1</span> <span style="color: #660033;">-O</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/\/$//'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #ff0000;">'{print $NF }'</span><span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>
<p>Sometimes you just need a big file. It doesn&#8217;t have to be interesting or contain any cosmological insights, but it does need to be a certain size. Pass that size in megabytes and this will give you just that, via a file full of zeroes:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><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;">function</span> fakefile <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;print '0' x 1024 x 1024 x $1&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$1</span>-MB-fake-file.txt<br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>
<p>This handy one extracts all email addresses in a file and gives you a unique sorted list:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><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;">alias</span> <span style="color: #007800;">findallemails</span>=<span style="color: #ff0000;">&quot;perl -wne'while(/[\w\.]+@[\w\.]+\w+/g){print <span style="color: #000099; font-weight: bold;">\&quot;</span>$&amp;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\&quot;</span>}' &quot;</span><br />
<span style="color: #000000; font-weight: bold;">function</span> findemails <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; findallemails <span style="color: #007800;">$1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">uniq</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>
<h2>Listing and searching</h2>
<p>When you invoke <a title="Post on ack-grep" href="http://dancingpenguinsoflight.com/2008/09/ack-grep-but-better/" target="_blank">ack-grep</a> it&#8217;s case-sensitive by default. It also ignores certain files and directories, based on their nature in some cases and in all cases where it doesn&#8217;t know what the filetype is. This incantation removes those limitations and is shorter to boot:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><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;">alias</span> <span style="color: #007800;">ack</span>=<span style="color: #ff0000;">&quot;ack-grep -i -u&quot;</span></div></td></tr></tbody></table></div>
<p>This series allows you to list only directories or only files. You can pass a directory to list the contents or it will use the current directory as default:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><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;"># Feeds file and directory filtered listers:</span><br />
<span style="color: #000000; font-weight: bold;">function</span> lister <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$1&quot;</span> == <span style="color: #ff0000;">''</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'.'</span>; <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;$1&quot;</span>; <span style="color: #000000; font-weight: bold;">fi</span><span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<span style="color: #666666; font-style: italic;"># List only directories:</span><br />
<span style="color: #000000; font-weight: bold;">function</span> lsd <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; lister <span style="color: #007800;">$1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">'^d'</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<span style="color: #666666; font-style: italic;"># More memorable version:</span><br />
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">lsdirs</span>=lsd<br />
<span style="color: #666666; font-style: italic;"># List only files:</span><br />
<span style="color: #000000; font-weight: bold;">function</span> lsf <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; lister <span style="color: #007800;">$1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-v</span> <span style="color: #ff0000;">'^d'</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<span style="color: #666666; font-style: italic;"># More memorable version:</span><br />
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">lsfiles</span>=lsf</div></td></tr></tbody></table></div>
<h2>Media and Fun</h2>
<p>These two require <a title="Man page for identify" href="http://cf.ccmr.cornell.edu/cgi-bin/w3mman2html.cgi?identify%281%29" target="_blank">identify</a> be installed, part of <a title="ImageMagick CLI" href="http://www.imagemagick.org/script/command-line-tools.php" target="_blank">ImageMagick</a>. Install the imagemagick package for your system first. Pass the filename of an image and the first will provide a range of information while the second will just provide the resolution:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><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;">alias</span> <span style="color: #007800;">imginfo</span>=<span style="color: #ff0000;">&quot;identify -format '-- %f -- <span style="color: #000099; font-weight: bold;">\n</span>Type: %m<span style="color: #000099; font-weight: bold;">\n</span>Size: %b bytes<span style="color: #000099; font-weight: bold;">\n</span>Resolution: %wpx x %hpx<span style="color: #000099; font-weight: bold;">\n</span>Colors: %k'&quot;</span><br />
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">imgres</span>=<span style="color: #ff0000;">&quot;identify -format '%f: %wpx x %hpx<span style="color: #000099; font-weight: bold;">\n</span>'&quot;</span></div></td></tr></tbody></table></div>
<p>Examples:<br />
<a href="http://dancingpenguinsoflight.com/wp-content/uploads/2011/05/imginfo.png"><img class="alignright size-full wp-image-1978" title="imginfo" src="http://dancingpenguinsoflight.com/wp-content/uploads/2011/05/imginfo.png" alt="" width="655" height="145" /></a><br />
Sometimes you just need to jack in. Or at least fill your screen with impressive looking output:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><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;">alias</span> <span style="color: #007800;">rmatrix</span>=<span style="color: #ff0000;">'echo -ne &quot;\e[31m&quot; ; while true ; do echo -ne &quot;\e[$(($RANDOM % 2 + 1))m&quot; ; tr -c &quot;[:print:]&quot; &quot; &quot; &lt; /dev/urandom | dd count=1 bs=50 2&gt; /dev/null ; done'</span><br />
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gmatrix</span>=<span style="color: #ff0000;">'echo -ne &quot;\e[32m&quot; ; while true ; do echo -ne &quot;\e[$(($RANDOM % 2 + 1))m&quot; ; tr -c &quot;[:print:]&quot; &quot; &quot; &lt; /dev/urandom | dd count=1 bs=50 2&gt; /dev/null ; done'</span></div></td></tr></tbody></table></div>
<p><a href="http://dancingpenguinsoflight.com/wp-content/uploads/2011/05/matrix.png"><img class="alignright size-full wp-image-1981" title="matrix" src="http://dancingpenguinsoflight.com/wp-content/uploads/2011/05/matrix.png" alt="" width="572" height="300" /></a></p>
<div style="clear: both;"></div>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2011/05/bag-of-useful-bash-aliases-and-functions/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Counting columns in a CSV from the CLI on Linux</title>
		<link>http://dancingpenguinsoflight.com/2011/01/counting-columns-in-a-csv-from-the-cli-on-linux/</link>
		<comments>http://dancingpenguinsoflight.com/2011/01/counting-columns-in-a-csv-from-the-cli-on-linux/#comments</comments>
		<pubDate>Sun, 09 Jan 2011 15:49:38 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[CLI]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1847</guid>
		<description><![CDATA[In wrangling some DB exports recently I needed the number of columns in multiple CSVs. It&#8217;s easy enough to just open them in OpenOffice and check the final column number, but I wanted a CLI utility. I even entertained some &#8230; <a href="http://dancingpenguinsoflight.com/2011/01/counting-columns-in-a-csv-from-the-cli-on-linux/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In wrangling some DB exports recently I needed the number of columns in multiple CSVs. It&#8217;s easy enough to just open them in OpenOffice and check the final column number, but I wanted a CLI utility. I even entertained some notions of wc having a magical option to provide this, but no luck there.</p>
<p>So here&#8217;s a simple function I added to my .bashrc to do the trick:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><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;"># Displays number of columns (intended for CSV-type files),</span><br />
<span style="color: #666666; font-style: italic;"># pass in separator then filename:</span><br />
<span style="color: #000000; font-weight: bold;">function</span> colcnt <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #007800;">DELIM</span>=<span style="color: #007800;">$1</span>;<br />
&nbsp; &nbsp; <span style="color: #007800;">FILE</span>=<span style="color: #007800;">$2</span>;<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;# of lines | Columns in <span style="color: #007800;">$FILE</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;(&gt;1 row means there are other separators in the file):&quot;</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span><span style="color: #ff0000;">&quot;<span style="color: #007800;">$DELIM</span>&quot;</span> <span style="color: #ff0000;">'{print NF}'</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${FILE}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">uniq</span> <span style="color: #660033;">-c</span> <br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>
<p><span id="more-1847"></span><br />
It&#8217;s in a function because Bash aliases don&#8217;t accept parameters. It uses the variable NF from awk which is the number of fields in the current record. The number of fields is sorted then unique entries are counted, providing a way to detect rows with different field separators or rows with missing columns. </p>
<p>Say you have a simple csv file called foo.csv:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&quot;i am&quot;,&quot;some cols&quot;,&quot;yay&quot;<br />
&quot;i am&quot;,&quot;some cols&quot;,&quot;yay&quot;<br />
&quot;i am&quot;,&quot;some cols&quot;,&quot;yay&quot;<br />
&quot;i am&quot;,&quot;some cols&quot;,&quot;yay&quot;<br />
&quot;i am&quot;|&quot;messssssed&quot;|&quot;up&quot;<br />
&quot;i am&quot;,&quot;some cols&quot;,&quot;yay&quot;<br />
&quot;i am&quot;,&quot;some cols&quot;,&quot;yay&quot;<br />
&quot;i am&quot;|&quot;messssssed&quot;|&quot;up&quot;</div></td></tr></tbody></table></div>
<p>After you add the function above to your .bashrc open a new shell (I found that sourcing .bashrc did not always update the function definition) and run this:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><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;">&gt;</span> colcnt , foo.csv <br />
<span style="color: #666666; font-style: italic;"># of lines | Columns in foo.csv</span><br />
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">1</span> row means there are other separators <span style="color: #000000; font-weight: bold;">in</span> the <span style="color: #c20cb9; font-weight: bold;">file</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">2</span> <span style="color: #000000;">1</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">6</span> <span style="color: #000000;">3</span></div></td></tr></tbody></table></div>
<p>This tells us that six lines had three columns and two lines had one column (as defined by the separator we passed in). If the CSV was consistently formatted (without the pipe-delimited line) you would only see one result row, being the total number of lines followed by the number of columns each line has (8 3).</p>
<p>If there&#8217;s a better/built-in way to do this please share! So far it&#8217;s been easy to use and helpful.</p>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2011/01/counting-columns-in-a-csv-from-the-cli-on-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handy tips for bash scripting: screen reminder and logging</title>
		<link>http://dancingpenguinsoflight.com/2010/12/handy-tips-for-bash-scripting-screen-reminder-and-logging/</link>
		<comments>http://dancingpenguinsoflight.com/2010/12/handy-tips-for-bash-scripting-screen-reminder-and-logging/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 20:37:55 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[CLI]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1727</guid>
		<description><![CDATA[Often I find myself writing simple wrapper scripts for long-running tasks, like migrating a database or performing some repair or optimization process. I mainly want to ensure proper logging is kept in order to determine how long each step takes &#8230; <a href="http://dancingpenguinsoflight.com/2010/12/handy-tips-for-bash-scripting-screen-reminder-and-logging/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Often I find myself writing simple wrapper scripts for long-running tasks, like migrating a database or performing some repair or optimization process. I mainly want to ensure proper logging is kept in order to determine how long each step takes and so I have a record (aside from avoiding executing each step manually more than once).</p>
<h2>Screen reminder</h2>
<p>Since these operations are also usually important I want to make sure I run them in <a target="_blank" title="man page for screen" href="http://linux.die.net/man/1/screen">screen</a> so they don&#8217;t screech to a halt if my connection happens to timeout or otherwise disconnect. The problem is that I create these scripts pretty organically, primarily aggregating commands I run step by step on test systems, adding some helpful output, etc. During the creation process I am jumping between different terminal views so running in screen isn&#8217;t on my mind. To save myself some heartburn from the potential of starting the real thing without being in screen I&#8217;ve taken to adding this to the beginning of such scripts:<br />
<span id="more-1727"></span><br />
[<strong>EDIT, 2010-12-19</strong>] Thanks to visitor Dominic I know a better way to implement this. I advise setting a variable indicating whether you are testing or not (this can be useful for other switches in your scripts as well). If not in testing, then check whether the user is running in screen or not. This allows us to be reminded only when we need it, which I much prefer! Here&#8217;s a version of what I just described:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><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/bash</span><br />
<span style="color: #666666; font-style: italic;"># Set to 1 if you are ready to run in production</span><br />
<span style="color: #007800;">PRODUCTION</span>=<span style="color: #000000;">1</span><br />
<br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$PRODUCTION</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$STY</span><span style="color: #000000; font-weight: bold;">`</span> == <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;WARNING: You are not running in <span style="color: #000099; font-weight: bold;">\<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp;screen. Please start screen and re-run.&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">exit</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
<span style="color: #000000; font-weight: bold;">fi</span></div></td></tr></tbody></table></div>
<p>If you aren&#8217;t in screen then $STY contains nothing. Just what we needed! What follows is the first version, which I want to preserve as an example of the handy select function. [END EDIT]</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><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/bash</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Are you running in screen?&quot;</span><br />
<span style="color: #000000; font-weight: bold;">select</span> yn <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">&quot;Yes&quot;</span> <span style="color: #ff0000;">&quot;No&quot;</span>; <span style="color: #000000; font-weight: bold;">do</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$yn</span> <span style="color: #000000; font-weight: bold;">in</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Yes <span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">break</span><span style="color: #000000; font-weight: bold;">;;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; No <span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">exit</span><span style="color: #000000; font-weight: bold;">;;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">esac</span><br />
<span style="color: #000000; font-weight: bold;">done</span></div></td></tr></tbody></table></div>
<p>This will provide a reminder to select no, enter screen, and run the script again:<br />
<a href="http://dancingpenguinsoflight.com/wp-content/uploads/2010/12/screen.png"><img src="http://dancingpenguinsoflight.com/wp-content/uploads/2010/12/screen.png" alt="" title="screen" width="308" height="106" class="aligncenter size-full wp-image-1788" /></a><br />
By the way, in case you haven&#8217;t been using <a target="_blank" title="More info on Bash select" href="http://www.softpanorama.org/Scripting/Shellorama/Control_structures/select_statements.shtml">select</a>, I recommend it. It provides a numerical selection instead of users entering words or letters. This gets rid of the need for you to parse input, exit based on invalid entries, etc.</p>
<h2>Dating your logs and watching them too</h2>
<p>In a lot of the same situations as the ones described above, I&#8217;ll have some basic log requirements I want to cover and usually want to determine the total time for a script to run. I also want to print out the logging entries at the same time as recording them to a file. All easily accomplished. Here&#8217;s a quick example:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><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/bash</span><br />
<span style="color: #007800;">LOG</span>=<span style="color: #ff0000;">&quot;tee -a scriptX.log&quot;</span><br />
<span style="color: #007800;">START</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #ff0000;">'%s'</span><span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #000000; font-weight: bold;">function</span> DATE <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #ff0000;">'%Y-%m-%d'</span>;<br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">function</span> DT <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #ff0000;">'%Y-%m-%d %H:%M:%S'</span>;<br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;-- <span style="color: #780078;">`DATE`</span>: Beginning operations!&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #007800;">$LOG</span><br />
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;-- <span style="color: #780078;">`DT`</span>: Detailed operation log 1&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #007800;">$LOG</span><br />
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;-- <span style="color: #780078;">`DT`</span>: Detailed operation log 2&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #007800;">$LOG</span><br />
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span><br />
<span style="color: #007800;">DURATION</span>=$<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #ff0000;">'%s'</span><span style="color: #000000; font-weight: bold;">`</span>-<span style="color: #007800;">$START</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;-- <span style="color: #780078;">`DATE`</span>: Operations complete!&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #007800;">$LOG</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;-- Operations took <span style="color: #007800;">$DURATION</span> seconds.&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #007800;">$LOG</span></div></td></tr></tbody></table></div>
<p>Resulting in:<br />
<a href="http://dancingpenguinsoflight.com/wp-content/uploads/2010/12/logging-run.png"><img src="http://dancingpenguinsoflight.com/wp-content/uploads/2010/12/logging-run.png" alt="" title="logging-run" width="434" height="127" class="aligncenter size-full wp-image-1998" /></a></p>
<p>[<strong>EDIT, 2011-05-15</strong>] Thanks to reading gullevek I realized I had this all wrong. You have to wrap the date invocations in functions so that when you call `DT` and `DATE` they are run each time. [END EDIT] This approach is useful for variables that will change through your script&#8217;s execution, like timestamps, without having to rewrite the same potentially complicated invocation each time. </p>
<p>START and DURATION, on the other hand, can be defined as plain variables and then simply called as $START and $DURATION since we only want them to be evaluated once (when bash evaluates the definition line). The %s format argument passed to date results in an output of seconds from the Unix epoch. This is done simply because it makes it easy to calculate duration by subtracting two of these. On that note, observe the format for arithmetic operations used to define DURATION. Bash is pretty picky about this way to do mathematical operations, so watch what you are doing.</p>
<p>I use the facilities described above pretty frequently. If you do much Bash scripting, you should consider adding the ones that look useful as snippets in your editor of choice.</p>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2010/12/handy-tips-for-bash-scripting-screen-reminder-and-logging/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Improved random password generator script</title>
		<link>http://dancingpenguinsoflight.com/2010/11/improved-random-password-generator-script/</link>
		<comments>http://dancingpenguinsoflight.com/2010/11/improved-random-password-generator-script/#comments</comments>
		<pubDate>Sun, 07 Nov 2010 15:33:01 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1714</guid>
		<description><![CDATA[[EDIT, 2010-11-10:] After being kindly corrected by Chris Jones, I recommend instead using a readily available utility that is more feature rich to boot: pwgen.[/EDIT] I have been using this script I wrote about a few years back to generate &#8230; <a href="http://dancingpenguinsoflight.com/2010/11/improved-random-password-generator-script/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>[EDIT, 2010-11-10:] After being kindly corrected by Chris Jones, I recommend instead using a readily available utility that is more feature rich to boot: <a href="http://linux.die.net/man/1/pwgen">pwgen</a>.[/EDIT]<br />
<span id="more-1714"></span><br />
I have been using <a target="_blank" href="http://dancingpenguinsoflight.com/2007/07/generate-random-passwords-on-command/">this script</a> I wrote about a few years back to generate random passwords. It works, but it&#8217;s limited. I&#8217;ve written a new one that doesn&#8217;t have requirements other than Python 2.5 or higher, is more flexible and a bit easier to follow: <a target="_blank" title="randpass.py on GitHub" href="https://github.com/shuckins/sph_code/blob/4919b1a3d9999678c0d6db2a769ee7de7cc6a526/sysadmin/misc/randpass.py">randpass.py</a>.</p>
<p>This script is very easy to use. Download it somewhere you keep handy scripts and make an alias like this:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><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;">alias</span> <span style="color: #007800;">randpass</span>=<span style="color: #ff0000;">&quot;~/code/code_homerepo/sysadmin/misc/randpass.py&quot;</span></div></td></tr></tbody></table></div>
<p>Source your .bashrc file (. ~/.bashrc) and try it out to quickly generate a password:<br />
<a href="http://dancingpenguinsoflight.com/wp-content/uploads/2010/11/randpass-examples.png"><img src="http://dancingpenguinsoflight.com/wp-content/uploads/2010/11/randpass-examples.png" alt="" title="randpass-examples" width="428" height="238" class="aligncenter size-full wp-image-1716" /></a></p>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2010/11/improved-random-password-generator-script/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Dealing with &#8220;could not create shared memory segment&#8221; from postgres on Ubuntu</title>
		<link>http://dancingpenguinsoflight.com/2010/03/dealing-with-could-not-create-shared-memory-segment-from-postgres-on-ubuntu/</link>
		<comments>http://dancingpenguinsoflight.com/2010/03/dealing-with-could-not-create-shared-memory-segment-from-postgres-on-ubuntu/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 14:44:49 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[CLI]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1458</guid>
		<description><![CDATA[After installing postgresql-8.4 from the Ubuntu repos on an Ubuntu 9.10 machine, I received the following when the daemon tried to start up: 1234567&#160;* The PostgreSQL server failed to start. Please check the log output: FATAL: &#160;could not create shared &#8230; <a href="http://dancingpenguinsoflight.com/2010/03/dealing-with-could-not-create-shared-memory-segment-from-postgres-on-ubuntu/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After installing postgresql-8.4 from the Ubuntu repos on an Ubuntu 9.10 machine, I received the following when the daemon tried to start up:</p>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;* The PostgreSQL server failed to start. Please check the log output:<br />
FATAL: &nbsp;could not create shared memory segment: Invalid argument<br />
DETAIL: &nbsp;Failed system call was shmget(key=5433001, size=35233792, 03600).<br />
HINT: &nbsp;This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMMAX parameter. &nbsp;You can either reduce the request size or reconfigure the kernel with larger SHMMAX. &nbsp;To reduce the request size (currently 35233792 bytes), reduce PostgreSQL's shared_buffers parameter (currently 4096) and/or its max_connections parameter (currently 13).<br />
&nbsp; &nbsp; If the request size is already small, it's possible that it is less than your kernel's SHMMIN parameter, in which case raising the request size or reconfiguring SHMMIN is called for.<br />
&nbsp; &nbsp; The PostgreSQL documentation contains more information about shared memory configuration.<br />
[fail]</div></td></tr></tbody></table></div>
<p>Fail indeed. Here&#8217;s the fix:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><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;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>sysctl.conf <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>sysctl.conf-orig <span style="color: #000000; font-weight: bold;">&amp;&amp;</span><br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">sh</span> <span style="color: #660033;">-c</span> <span style="color: #ff0000;">'echo &quot;#\n# For postgres\nkernel.shmmax = 104857600&quot; &gt;&gt; /etc/sysctl.conf'</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span><br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> sysctl <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span><br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>postgresql-<span style="color: #000000;">8.4</span> start</div></td></tr></tbody></table></div>
<p>This increases a particular kernel parameter to allow postgres enough memory to start. Yay for poor defaults&#8230;</p>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2010/03/dealing-with-could-not-create-shared-memory-segment-from-postgres-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Converting multiple images into one PDF on Linux</title>
		<link>http://dancingpenguinsoflight.com/2010/02/converting-multiple-images-to-one-pdf-on-linux/</link>
		<comments>http://dancingpenguinsoflight.com/2010/02/converting-multiple-images-to-one-pdf-on-linux/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 18:12:26 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[CLI]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PDF]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1440</guid>
		<description><![CDATA[There are a number of ways you can go about converting image files into PDFs, most simply by opening a given image and printing it. You can select &#8220;Print to file&#8221; instead of a printer device, then select PDF format &#8230; <a href="http://dancingpenguinsoflight.com/2010/02/converting-multiple-images-to-one-pdf-on-linux/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>There are a number of ways you can go about converting image files into PDFs, most simply by opening a given image and printing it. You can select &#8220;Print to file&#8221; instead of a printer device, then select PDF format and give it a name. But what to do if you have a folder full of images and want to make one big PDF of them? Here&#8217;s how you can do that:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><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;">cd</span> FOLDER_WITH_IMAGES<br />
convert <span style="color: #000000; font-weight: bold;">*</span>.jpg images.pdf</div></td></tr></tbody></table></div>
<p>This creates a single PDF, images.pdf, containing all JPG files in the directory. It uses the very handy <a title="convert man page" href="http://linux.die.net/man/1/convert" target="_blank">convert</a> utility. Thanks to reader andreas for pointing out this much simpler approach!</p>
<p>[Edit, 2011-08-18] Thanks to reader A. Syukri for pointing out that the above approach can crash when there are too many input files. Here&#8217;s the longer approach that should be more stable:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><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;">cd</span> FOLDER_WITH_IMAGES <br />
<span style="color: #007800;">FILES</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-type</span> f <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;*jpg&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> -d<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-f</span> <span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> temp <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">cd</span> temp <br />
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$FILES</span>; <span style="color: #000000; font-weight: bold;">do</span> <br />
&nbsp; &nbsp; <span style="color: #007800;">BASE</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$file</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/.jpg//g'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;<br />
&nbsp; &nbsp; convert ..<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$BASE</span>.jpg <span style="color: #007800;">$BASE</span>.pdf; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <br />
pdftk <span style="color: #000000; font-weight: bold;">*</span>pdf <span style="color: #c20cb9; font-weight: bold;">cat</span> output ..<span style="color: #000000; font-weight: bold;">/</span>FINAL_NAME.pdf <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <br />
<span style="color: #7a0874; font-weight: bold;">cd</span> .. <br />
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> temp</div></td></tr></tbody></table></div>
<p>This loops through all the .jpg images in the directory and converts them to a PDF file of the same name. Once that is done they are all combined into FINAL_NAME.pdf by <a title="pdftk home" href="http://www.accesspdf.com/pdftk/" target="_blank">pdftk</a>, a handy PDF utility. The temp dir business is there to make the temp PDF file removal easier.</p>
<p>If you need to do this frequently you can create a bash function and pass in the folder containing the JPGs. You might want to also consider passing in the extension(s) desired if that varies for you.</p>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2010/02/converting-multiple-images-to-one-pdf-on-linux/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Fun with ASCII art</title>
		<link>http://dancingpenguinsoflight.com/2009/12/fun-with-ascii-art/</link>
		<comments>http://dancingpenguinsoflight.com/2009/12/fun-with-ascii-art/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 06:06:02 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[CLI]]></category>
		<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1381</guid>
		<description><![CDATA[No, in fact ASCII art isn&#8217;t dead, it&#8217;s all the rage. Now that that&#8217;s established, check out these handy programs for generating your own ASCII art messages: 1figlet -f smslant Boxes rules. &#124; boxes -d parchment -a hcvc Figure 1. &#8230; <a href="http://dancingpenguinsoflight.com/2009/12/fun-with-ascii-art/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>No, in fact <a title="ASCII art on Wikipedia" href="http://en.wikipedia.org/wiki/Ascii_art" target="_blank">ASCII art</a> isn&#8217;t dead, it&#8217;s all the rage. Now that that&#8217;s established, check out these handy programs for generating your own ASCII art messages:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">figlet <span style="color: #660033;">-f</span> smslant Boxes rules. <span style="color: #000000; font-weight: bold;">|</span> boxes <span style="color: #660033;">-d</span> parchment <span style="color: #660033;">-a</span> hcvc</div></td></tr></tbody></table></div>
<p><img class="aligncenter size-full wp-image-1390" title="boxes" src="http://dancingpenguinsoflight.com/wp-content/uploads/2009/12/boxes.png" alt="boxes" width="435" height="144" /></p>
<p style="text-align: center;">Figure 1. <em>boxes</em> provides boundless entertainment.</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">figlet <span style="color: #660033;">-f</span> standard Also figlet. <span style="color: #000000; font-weight: bold;">|</span> boxes <span style="color: #660033;">-d</span> mouse <span style="color: #660033;">-a</span> hcvc</div></td></tr></tbody></table></div>
<p><img class="aligncenter size-full wp-image-1391" title="figlet" src="http://dancingpenguinsoflight.com/wp-content/uploads/2009/12/figlet.png" alt="figlet" width="417" height="266" /></p>
<p style="text-align: center;">Figure 2. <em>figlet</em> and its inherent awesomeness.</p>
<p>Spice up those bland Bash scripts with entertaining comment formats!</p>
<p>But seriously, these utilities can make for more interesting banners, alert messages, and so on:</p>
<ul>
<li><a title="boxes homepage" href="http://boxes.thomasjensen.com/" target="_blank">boxes</a>: Creates ASCII art boxes around entered text</li>
<li><a title="figlet homepage" href="http://www.figlet.org/" target="_blank">figlet</a>: Creates large ASCII art versions of entered characters</li>
</ul>
<p>Just because it&#8217;s the command line doesn&#8217;t mean it has to be boring!</p>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2009/12/fun-with-ascii-art/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Viewing all users on a Linux system</title>
		<link>http://dancingpenguinsoflight.com/2009/10/viewing-all-users-on-a-linux-system/</link>
		<comments>http://dancingpenguinsoflight.com/2009/10/viewing-all-users-on-a-linux-system/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 20:35:59 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[CLI]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1274</guid>
		<description><![CDATA[There are a number of widely-used and stable utilities on Linux systems that allow you to view information related to users. You can see who&#8217;s logged in with who, get info on a particular user with finger, see who you &#8230; <a href="http://dancingpenguinsoflight.com/2009/10/viewing-all-users-on-a-linux-system/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>There are a number of widely-used and stable utilities on Linux systems that allow you to view information related to users. You can see who&#8217;s logged in with <a href="http://linux.die.net/man/1/who">who</a>, get info on a particular user with <a href="http://linux.die.net/man/1/finger">finger</a>, see who you are with <a href="http://linux.die.net/man/1/whoami">whoami</a> and see who logged in last with <a href="http://linux.die.net/man/1/last">last</a> and <a href="http://linux.die.net/man/8/lastlog">lastlog</a>. And there are commands to create, remove, and change users and their groups. </p>
<p>But what if you just want to see all the users defined on the entire system? Looking in /home won&#8217;t show you users that don&#8217;t have a home dir or one not located there. All users are indeed listed in <a target="_blank" title="Article on /etc/passwd format" href="http://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/">/etc/passwd</a>, but having to look through this file every time you just want a list of users is tedious. I haven&#8217;t found a utility that performs this role. </p>
<p>This was somewhat surprising, as it&#8217;s not an uncommon need. Perhaps I&#8217;ve missed some essential program along my Linux journey. If so, feel free to enlighten me in a comment <img src='http://dancingpenguinsoflight.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  In the meantime, let&#8217;s fill this gap:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><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;"># Prints all users, divided by login ability and homedir:</span><br />
<span style="color: #000000; font-weight: bold;">function</span> userinfo <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;----- Users that can login -----&quot;</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span><span style="color: #ff0000;">&quot;:&quot;</span> <span style="color: #ff0000;">'!/bin\/false/ { print &quot;username: &quot; $1 &quot;, uid: &quot; $3 &quot;, homedir: &quot; $6 }'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">passwd</span><br />
<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>----- And have /home dir -----&quot;</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span><span style="color: #ff0000;">&quot;:&quot;</span> <span style="color: #ff0000;">'!/bin\/false/ &amp;&amp; /\/home/ { print &quot;username: &quot; $1 &quot;, uid: &quot; $3 &quot;, homedir: &quot; $6 }'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">passwd</span><br />
<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>----- Users that can't login -----&quot;</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span><span style="color: #ff0000;">&quot;:&quot;</span> <span style="color: #ff0000;">'/\/bin\/false/ { print &quot;username: &quot; $1 &quot;, uid: &quot; $3 &quot;, homedir: &quot; $6 }'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">passwd</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>
<p>This function should work on any *nix system. Place it inside your ~/.bashrc, reload that (. ~/.bashrc), and try out &#8220;userinfo&#8221;:</p>
<p><img src="http://dancingpenguinsoflight.com/wp-content/uploads/2009/10/userinfo.png" alt="userinfo" title="userinfo" width="534" height="645" class="aligncenter size-full wp-image-1280" /></p>
<p>Three sections are displayed showing usernames, user IDs, and homedirs for users in /etc/passwd who:</p>
<ul>
<li>Don&#8217;t have /bin/false for a shell (i.e. users that can login)</li>
<li>Also have a homedir under /home (i.e. the users that aren&#8217;t for system processes)</li>
<li>Do have /bin/false for a shell (i.e. users that can&#8217;t login)</li>
</ul>
<p>Note that the users that can login category only shows users to which one could switch with &#8220;sudo su &#8211; USERNAME&#8221; from a shell. This doesn&#8217;t mean anything about whether they can login via SSH or other access methods.</p>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2009/10/viewing-all-users-on-a-linux-system/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Get useful image information on the command line</title>
		<link>http://dancingpenguinsoflight.com/2009/09/get-useful-image-information-on-the-command-line/</link>
		<comments>http://dancingpenguinsoflight.com/2009/09/get-useful-image-information-on-the-command-line/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 03:20:57 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[CLI]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Linuc]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1212</guid>
		<description><![CDATA[ImageMagick is an incredible set of programs that allow you to get all sorts of information on image files from the command line, as well as batch edit and generally alter images of many varieties. You can read all about &#8230; <a href="http://dancingpenguinsoflight.com/2009/09/get-useful-image-information-on-the-command-line/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" title="ImageMagick home" href="http://www.imagemagick.org/script/index.php">ImageMagick</a> is an incredible set of programs that allow you to get all sorts of information on image files from the command line, as well as batch edit and generally alter images of many varieties. You can read all about these facilities on ImageMagick&#8217;s <a target="_blank" title="ImageMagick command line tools" href="http://www.imagemagick.org/script/command-line-tools.php">well-made documentation site</a>.</p>
<p>What I often want, however, is simple: a command to get commonly desired information from any image file. Alias to the rescue!</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><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;">alias</span> <span style="color: #007800;">imginfo</span>=<span style="color: #ff0000;">&quot;identify -format '-- %f -- <span style="color: #000099; font-weight: bold;">\n</span>Type: %m<span style="color: #000099; font-weight: bold;">\n</span>Size: %b bytes<span style="color: #000099; font-weight: bold;">\n</span>Resolution: %wpx x %hpx<span style="color: #000099; font-weight: bold;">\n</span>Colors: %k'&quot;</span></div></td></tr></tbody></table></div>
<p>This produces a succinctly helpful output:</p>
<p><img src="http://dancingpenguinsoflight.com/wp-content/uploads/2009/09/imginfo.png" alt="imginfo" title="imginfo" width="415" height="101" class="aligncenter size-full wp-image-1216" /></p>
<p>Often this is all I need for basic tasks. And if more is needed there&#8217;s always the full ImageMagick suite.</p>
<p>You can alter the format to whatever you prefer, using <a target="_blank" title="ImageMagick format options" href="http://www.imagemagick.org/script/escape.php">the wide variety of variables</a>.</p>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2009/09/get-useful-image-information-on-the-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Useful Bash functions to determine OS and more</title>
		<link>http://dancingpenguinsoflight.com/2009/09/useful-bash-functions-to-determine-os-and-more/</link>
		<comments>http://dancingpenguinsoflight.com/2009/09/useful-bash-functions-to-determine-os-and-more/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 19:07:09 +0000</pubDate>
		<dc:creator>Samuel Huckins</dc:creator>
				<category><![CDATA[CLI]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://dancingpenguinsoflight.com/?p=1185</guid>
		<description><![CDATA[In a number of my Bash aliases I need to check two constants: what sort of OS I&#8217;m on and whether it&#8217;s a production box or not. I use the former for aliases that allow me to install and search &#8230; <a href="http://dancingpenguinsoflight.com/2009/09/useful-bash-functions-to-determine-os-and-more/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In a number of my Bash aliases I need to check two constants: what sort of OS I&#8217;m on and whether it&#8217;s a production box or not. I use the former for aliases that allow me to install and search for packages across distributions, <a target="_blank" title="Post on package installation aliases" href="http://dancingpenguinsoflight.com/2008/12/multi-os-package-installation-and-search-functions/">as described here</a>. The latter I use to determine whether I am allowed to use aliases for turning off the machine and the like. The code I used to determine these before was repetitive and hard to work with. So I created functions to determine the values, allowing all the aliases to reference a single source.</p>
<p>The first function determines the OS I am on, in the general categories of &#8220;debian&#8221; and &#8220;redhat&#8221;:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><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;">function</span> cur_os <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Attempts to check whether the local box is running Debian/Ubuntu</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># (considered together as &quot;debian&quot;) or Red Hat/CentOS (considered</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># together as &quot;redhat&quot;.</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Make sure we have egrep</span><br />
&nbsp; &nbsp; <span style="color: #007800;">EGREP_VER</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">--version</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">head</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${EGREP_VER:0:8}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;GNU grep&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;egrep isn't installed, sorry.&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Make sure we have /etc/issue</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">'/etc/issue'</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;/etc/issue isn't readable.&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Run checks</span><br />
&nbsp; &nbsp; <span style="color: #007800;">DEB_OS</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-i</span> <span style="color: #ff0000;">'Ubuntu|Debian'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>issue<span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; <span style="color: #007800;">RH_OS</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #660033;">-i</span> <span style="color: #ff0000;">'CentOS|Red Hat'</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>issue<span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #800000;">${#DEB_OS}</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">CUR_OS</span>=<span style="color: #ff0000;">&quot;debian&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #800000;">${#RH_OS}</span> <span style="color: #660033;">-gt</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">CUR_OS</span>=<span style="color: #ff0000;">&quot;redhat&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">CUR_OS</span>=<span style="color: #ff0000;">&quot;unknown&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$CUR_OS</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>
<p>I chose these two categories because the package handler is the same within each. This could of course be expanded to more systems, these just happen to be the flavors I am always on. Just after I define this function I also include:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">cur_os <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null</div></td></tr></tbody></table></div>
<p>This way the variable CUR_OS is populated each time Bash loads but the output doesn&#8217;t dirty up the prompt. Having this variable allows the definition of fairly elegant functions such as:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><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;">function</span> inst <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Installs passed packages.</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$CUR_OS</span>&quot;</span> <span style="color: #000000; font-weight: bold;">in</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">'debian'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get install</span> $<span style="color: #000000; font-weight: bold;">@</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">;;</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">'redhat'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">yum install</span> $<span style="color: #000000; font-weight: bold;">@</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">;;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You are not on a supported OS.&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">;;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">esac</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>
<p>This next function determines whether the current machine is one I consider non-production:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><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;">function</span> local_box <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Checks if the current machine is one of my own machines (and thus</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># less risky to run various commands on).</span><br />
&nbsp; &nbsp; <span style="color: #007800;">LOCAL_BOXES</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #ff0000;">'ZenSam'</span> <span style="color: #ff0000;">'B74kb0x'</span> <span style="color: #ff0000;">'MediaServer'</span> <span style="color: #ff0000;">'shuckins-alienware'</span> <span style="color: #ff0000;">'laptop-shuckins'</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #007800;">CURRENTBOX</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">hostname</span><span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; <span style="color: #007800;">CUR_BOX_LOCAL</span>=<span style="color: #000000;">0</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${LOCAL_BOXES[@]}</span>; <span style="color: #000000; font-weight: bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$i</span> == <span style="color: #800000;">${CURRENTBOX}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> ; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">CUR_BOX_LOCAL</span>=<span style="color: #000000;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">done</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$CUR_BOX_LOCAL</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<span style="color: #666666; font-style: italic;"># So that CUR_BOX_LOCAL is set initially:</span><br />
local_box <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null</div></td></tr></tbody></table></div>
<p>The idea is that LOCAL_BOXES holds hostnames of machines that aren&#8217;t as sensitive to things like shutting down. Then when you create aliases for such potentially destructive actions you define a safe form for the potentially sensitive boxes:</p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><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;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$CUR_BOX_LOCAL</span> == <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><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;">alias</span> <span style="color: #007800;">off</span>=<span style="color: #ff0000;">&quot;sudo shutdown -h now&quot;</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">restart</span>=<span style="color: #ff0000;">&quot;sudo shutdown -r now&quot;</span><br />
<span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">off</span>=<span style="color: #ff0000;">&quot;echo 'You are on a remote box!'&quot;</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">restart</span>=<span style="color: #ff0000;">&quot;echo 'You are on a remote box!'&quot;</span><br />
<span style="color: #000000; font-weight: bold;">fi</span></div></td></tr></tbody></table></div>
    ]]></content:encoded>
			<wfw:commentRss>http://dancingpenguinsoflight.com/2009/09/useful-bash-functions-to-determine-os-and-more/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

