<?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>#! &#187; Bash</title>
	<atom:link href="http://hash-bang.net/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://hash-bang.net</link>
	<description>The start of all wonderful things</description>
	<lastBuildDate>Sun, 15 May 2011 03:36:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Simple bash variable security with OpenSSL</title>
		<link>http://hash-bang.net/2010/11/bash-security-with-openssl/</link>
		<comments>http://hash-bang.net/2010/11/bash-security-with-openssl/#comments</comments>
		<pubDate>Wed, 17 Nov 2010 05:25:10 +0000</pubDate>
		<dc:creator>mc</dc:creator>
				<category><![CDATA[HowTo's]]></category>
		<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://hash-bang.net/?p=171</guid>
		<description><![CDATA[Lets say you have a script that requires a number of variables to operate such as a database connection that requires a server, username and password. It&#8217;s usually a good idea to keep all this config in one place but dumping this in a plain text file is usually a bad idea. The example below [...]]]></description>
			<content:encoded><![CDATA[<p>Lets say you have a script that requires a number of variables to operate such as a database connection that requires a server, username and password. It&#8217;s usually a good idea to keep all this config in one place but dumping this in a plain text file is usually a bad idea.<br />
The example below allows you to place application level config into a decently encrypted file and keep it all behind one master password.<br />
To do all this we need two files. The first is obviously the encrypted variable config itself. The second is your script file which needs to decrypt the config file and perform whatever action is needed.</p>
<p>In this example I&#8217;m going to assume we are connecting to a remote database. Most protocols require three pieces of data to pull this off: a server, a username and a password. We are going to store these three pieces of data in a file called <i>config.plain</i>, encrypt this file (making <i>config.aes</i>), extract its variables and finally connect to our remote database.</p>
<p>I&#8217;m going to use the following example <i>config.plain</i> file:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://hash-bang.net/wp-content/plugins/wp-codebox/wp-codebox.php?p=171&amp;download=config.plain">config.plain</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1717"><td class="code" id="p171code7"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">SERVER</span>=FooBar
<span style="color: #007800;">USER</span>=JOE
<span style="color: #007800;">PASS</span>=RANDOM</pre></td></tr></table></div>

<p>So we need to encrypt our config.plain file and turn it into config.aes (AES being the encryption standard we are using). The following example is cheerfully swiped from the example given over at <a href="http://tombuntu.com/index.php/2007/12/12/simple-file-encryption-with-openssl/">Tombuntu</a>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p171code8'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1718"><td class="code" id="p171code8"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&gt;</span>openssl aes-<span style="color: #000000;">256</span>-cbc <span style="color: #660033;">-a</span> <span style="color: #660033;">-salt</span> <span style="color: #660033;">-in</span> config.plain <span style="color: #660033;">-out</span> config.aes</pre></td></tr></table></div>

<p>So if we wanted to reverse this and decrypt the config.aes file on the command line we could run:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p171code9'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1719"><td class="code" id="p171code9"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&gt;</span>openssl aes-<span style="color: #000000;">256</span>-cbc <span style="color: #660033;">-d</span> <span style="color: #660033;">-a</span> <span style="color: #660033;">-salt</span> <span style="color: #660033;">-in</span> config.aes</pre></td></tr></table></div>

<p><b>NOTE:</b> For some reason the OpenSSL CLI doesn&#8217;t obey the Unix standard of specifying &#8216;-&#8217; as a file name to output to the console. Omitting the &#8216;-out&#8217; argument will instead output to STDOUT.</p>
<h1>Putting it all together</h1>
<p>The <i>config.plain</i> example file:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://hash-bang.net/wp-content/plugins/wp-codebox/wp-codebox.php?p=171&amp;download=config.plain">config.plain</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17110"><td class="code" id="p171code10"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">SERVER</span>=FooBar
<span style="color: #007800;">USER</span>=JOE
<span style="color: #007800;">PASS</span>=RANDOM</pre></td></tr></table></div>

<p>The <i>config.aes</i> file (same as above but after encryption using the password &#8216;password&#8217;):</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://hash-bang.net/wp-content/plugins/wp-codebox/wp-codebox.php?p=171&amp;download=config.aes">config.aes</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17111"><td class="code" id="p171code11"><pre class="text" style="font-family:monospace;">U2FsdGVkX19aTdTnSuV1p5R6DCQxp21yrlCc+9j9dap/ziojbiDP1SbSM+6HXF0T
PabsGQgSdV8Ir4rmMjgyFA==</pre></td></tr></table></div>

<p>The actual <i>script</i> file which does the decryption and performs the final actions on the data.<br />
We are going to accept the master password from the command line (so we would run the below as &#8216;script password&#8217; &#8211; assuming our script was called &#8216;script&#8217; and the password was &#8216;password&#8217;). This is not the most secure method available since you can see the launch method of all applications running on the system using &#8216;ps&#8217;. If you want more security I would instead store your password in a system variable and use that instead of reading from the command line. If you wish to do this you can simply strip out all the lines that do error checking and use &#8216;KEY&#8217; as a system variable to store the master password.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://hash-bang.net/wp-content/plugins/wp-codebox/wp-codebox.php?p=171&amp;download=script">script</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p17112"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p171code12"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># USAGE: script &lt;password&gt;</span>
<span style="color: #666666; font-style: italic;"># Do something with the associated config.aes script in a reasonably secure way</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Example config.plain content:</span>
<span style="color: #666666; font-style: italic;"># SERVER=FooBar</span>
<span style="color: #666666; font-style: italic;"># USER=JOE</span>
<span style="color: #666666; font-style: italic;"># PASS=RANDOM</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Command to encrypt config.plain -&gt; config.aes</span>
<span style="color: #666666; font-style: italic;"># openssl aes-256-cbc -a -salt -in config.plain -out config.aes</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Do some housekeeping to check we launched with the requisite number of arguments</span>
<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;">-ne</span> <span style="color: #000000;">1</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;">&quot;USAGE: $0 &lt;password&gt;&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #007800;">KEY</span>=<span style="color: #ff0000;">&quot;$1&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Import the variables from our decrypted stream</span>
<span style="color: #007800;">STREAM</span>=<span style="color: #000000; font-weight: bold;">`</span>openssl aes-<span style="color: #000000;">256</span>-cbc <span style="color: #660033;">-d</span> <span style="color: #660033;">-pass</span> <span style="color: #ff0000;">&quot;pass:<span style="color: #007800;">$KEY</span>&quot;</span> <span style="color: #660033;">-a</span> <span style="color: #660033;">-salt</span> <span style="color: #660033;">-in</span> config.aes<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;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #000000;">0</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;">&quot;Invalid password specified to decrypt configuration file. Aborting.&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$STREAM</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># REPLACE THE STUFF BELOW THIS LINE WITH WHAT YOU WANT TO DO WITH YOUR SECURE VARIABLES</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Server: <span style="color: #007800;">$SERVER</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Username: <span style="color: #007800;">$USER</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Password: <span style="color: #007800;">$PASS</span>&quot;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://hash-bang.net/2010/11/bash-security-with-openssl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Last.FM submissions with Audacious</title>
		<link>http://hash-bang.net/2010/06/lastfm-with-audacious/</link>
		<comments>http://hash-bang.net/2010/06/lastfm-with-audacious/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 00:35:10 +0000</pubDate>
		<dc:creator>mc</dc:creator>
				<category><![CDATA[Fixes]]></category>
		<category><![CDATA[Audacious]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://hash-bang.net/?p=162</guid>
		<description><![CDATA[Some time ago the good folks behind my favorite media player Audacious decided to abandon in-built support for Last.FM submissions. The reasoning behind this seems to be that the Last.FM API was becoming a little too much to maintain which I suppose is fair enough in its quest to remain just a music player i.e. [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago the good folks behind my favorite media player <a href="http://audacious-media-player.org/">Audacious</a> decided to abandon in-built support for Last.FM submissions.</p>
<p>The reasoning behind this seems to be that the Last.FM API was <a href="https://bugs.launchpad.net/ubuntu/+source/audacious-plugins/+bug/568651">becoming a little too much to maintain</a> which I suppose is fair enough in its quest to remain <em>just a music player</em> i.e. not a screen-hogging iTunes clone like so many others.</p>
<p>Heres how to put Last.FM support back into Audacity.</p>
<p>I&#8217;m going to assume you are a sensible person and using a Debian based system rather than Fedora. If you are one of these hat loving deviants, substitute &#8216;yum&#8217; for apt-get below and feel free to voyage though the countless pains required to get Yum to actually do its job.</p>
<h2>Installing LastFMSubmitD</h2>
<p>I&#8217;m not sure on the capitalization of the daemon process LastFMSubmitD so that might be the wrong name entirely.</p>
<p>Anyway, first install pretty much the only component we need:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p162code16'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16216"><td class="code" id="p162code16"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> lastfmsubmitd</pre></td></tr></table></div>

<h2>OPTIONAL &#8211; Set up the HTTP_PROXY</h2>
<p>For some reason the daemon does not get proxy details right. This is easily fixed by opening <em>/etc/init.d/lastfmsubmitd</em> in your favorite editor and inserting the following line somewhere near the top (I put it after the &#8216;GROUP=&#8217; line which should be the last bit of the daemon config area).<br />
If you have no idea what a proxy is or why you should be doing this you can probably skip this section.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p162code17'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16217"><td class="code" id="p162code17"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">http_proxy</span>=<span style="color: #ff0000;">&quot;http://whatever.stupid.proxy.edu.au:8080/&quot;</span></pre></td></tr></table></div>

<h2>Parse Audacious output</h2>
<p>Dump the following script somewhere you can find it. For this example I will use <em>/home/user/bin/scrobble</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://hash-bang.net/wp-content/plugins/wp-codebox/wp-codebox.php?p=162&amp;download=scrobble">scrobble</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16218"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p162code18"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-x</span> <span style="color: #ff0000;">'/usr/lib/lastfmsubmitd/lastfmsubmit'</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;">&quot;LastFMSubmitD does not appear to exist on this machine&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;You can install it using Apt or Yum&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<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: #000000;">0</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;">'Usage: scrobble &quot;&lt;artist&gt; - &lt;song&gt;&quot; &quot;&lt;length&gt;&quot;'</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">ARTIST</span>=<span style="color: #800000;">${1%% - *}</span>
<span style="color: #007800;">SONG</span>=<span style="color: #800000;">${1##* - }</span>
<span style="color: #007800;">LENGTH</span>=$<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$2</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">1000</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;ARTIST = [<span style="color: #007800;">$ARTIST</span>]&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;SONG = [<span style="color: #007800;">$SONG</span>]&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;LENGTH = [<span style="color: #007800;">$LENGTH</span>]&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>lastfmsubmitd<span style="color: #000000; font-weight: bold;">/</span>lastfmsubmit <span style="color: #660033;">--artist</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$ARTIST</span>&quot;</span> <span style="color: #660033;">--title</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$SONG</span>&quot;</span> <span style="color: #660033;">--length</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$LENGTH</span>&quot;</span></pre></td></tr></table></div>

<h2>Pointing Song Change at your script</h2>
<p>First make sure you have the Song Change plugin enabled in Audacious first.</p>
<p>Now point the command Audacious is to run when starting a new song at the script in the following way:</p>
<pre>
/home/user/bin/scrobble "%s" "%l"
</pre>
<p>All done. Now any tracks played via Audacious will be passed to the script which will parse the incoming song title into a format that the LastFMSubmitD daemon can use.</p>
]]></content:encoded>
			<wfw:commentRss>http://hash-bang.net/2010/06/lastfm-with-audacious/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Trash with Bash</title>
		<link>http://hash-bang.net/2009/03/trash-with-bash/</link>
		<comments>http://hash-bang.net/2009/03/trash-with-bash/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 05:15:15 +0000</pubDate>
		<dc:creator>mc</dc:creator>
				<category><![CDATA[HowTo's]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://hash-bang.net/?p=121</guid>
		<description><![CDATA[One of the regrettably unavoidable aspects of the Unix shell like environments is the impedance between what a user says and what a user means. While this is present in all computing environments the sheer power of Unix based command lines make a potential mistake catastrophic. Who amongst us has not at some point done [...]]]></description>
			<content:encoded><![CDATA[<p>One of the regrettably unavoidable aspects of the Unix shell like environments is the impedance between what a user <i>says</i> and what a user <i>means</i>. While this is present in all computing environments the sheer power of Unix based command lines make a potential mistake catastrophic.</p>
<p>Who amongst us has not at some point done something like:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p121code22'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12122"><td class="code" id="p121code22"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #000000; font-weight: bold;">*</span> .tmp</pre></td></tr></table></div>

<p>or</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p121code23'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12123"><td class="code" id="p121code23"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #000000; font-weight: bold;">*&gt;</span>tmp</pre></td></tr></table></div>

<p>The former makes the mistake of a space between the dot and the &#8216;tmp&#8217; part and the latter the accidental holding down of the shift key while pressing the intended full stop.</p>
<p>While searching for an alternative for the I-have-done-this-too-many-times-and-now-its-embarrassing approach of nuking everything with the &#8216;rm&#8217; command its time i did something about it.</p>
<p>Inserting the following in your ~/.bashrc file will remap the &#8216;rm&#8217; command to a slightly safer move-to-trash like behavior. Its not comprehensive but it has saved my life on more than one occasion.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://hash-bang.net/wp-content/plugins/wp-codebox/wp-codebox.php?p=121&amp;download=.bashrc">.bashrc</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12124"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p121code24"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Trash support by Matt Carter &lt;m@ttcarter.com</span>
<span style="color: #666666; font-style: italic;"># Source and information: http://hash-bang.net/2009/03/trash-with-bash</span>
<span style="color: #000000; font-weight: bold;">function</span> trash<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>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$HOME</span>/.trashcan/$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
		<span style="color: #666666; font-style: italic;"># Already exists in bin - remove</span>
		<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$HOME</span>/.trashcan/$1&quot;</span>
	<span style="color: #000000; font-weight: bold;">fi</span>
	<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #660033;">--target-directory</span>=<span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>.trashcan<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">--</span> <span style="color: #ff0000;">&quot;$@&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #ff0000;">&quot;rm=trash&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #ff0000;">&quot;emptytrash=/bin/rm -rf <span style="color: #007800;">$HOME</span>/.trashcan/* <span style="color: #007800;">$HOME</span>/.trashcan/.??*&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #ff0000;">'rm!=/bin/rm -r'</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> ~<span style="color: #000000; font-weight: bold;">/</span>.trashcan <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null</pre></td></tr></table></div>

<p>Now the command &#8216;rm&#8217; moves files into ~/.trashcan. You can also use the command &#8216;rm!&#8217; when you really mean delete immediately and the utility command &#8216;emptytrash&#8217; to clean everything out.</p>
]]></content:encoded>
			<wfw:commentRss>http://hash-bang.net/2009/03/trash-with-bash/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Zapping processes in Bash</title>
		<link>http://hash-bang.net/2009/01/zapping-processes-in-bash/</link>
		<comments>http://hash-bang.net/2009/01/zapping-processes-in-bash/#comments</comments>
		<pubDate>Sat, 24 Jan 2009 00:14:15 +0000</pubDate>
		<dc:creator>mc</dc:creator>
				<category><![CDATA[HowTo's]]></category>
		<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://hash-bang.net/?p=89</guid>
		<description><![CDATA[The following is a handy little script I wrote which politely asks a collection of matching processes to exit. After a second the process is forcibly killed. This command is designed as a drop-in replacement for the slightly cryptic pgrep and pkill commands. ?Download zap.sh1 2 3 4 5 6 7 8 9 10 11 [...]]]></description>
			<content:encoded><![CDATA[<p>The following is a handy little script I wrote which politely asks a collection of matching processes to exit. After a second the process is forcibly killed.</p>
<p>This command is designed as a drop-in replacement for the slightly cryptic pgrep and pkill commands.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://hash-bang.net/wp-content/plugins/wp-codebox/wp-codebox.php?p=89&amp;download=zap.sh">zap.sh</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8928"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code" id="p89code28"><pre class="bash" style="font-family:monospace;">zap<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>
	<span style="color: #666666; font-style: italic;"># Usage: zap [SIGNAL|-NUMERIC|shoot] &lt;fgrep&gt;</span>
	<span style="color: #007800;">SIGNAL</span>=<span style="color: #ff0000;">''</span>
	<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">in</span>
		shoot<span style="color: #000000; font-weight: bold;">|</span>-<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">SIGNAL</span>=<span style="color: #ff0000;">'SHOOT'</span><span style="color: #000000; font-weight: bold;">;;</span>
		term<span style="color: #000000; font-weight: bold;">|</span>-<span style="color: #000000;">15</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">SIGNAL</span>=<span style="color: #ff0000;">'TERM'</span><span style="color: #000000; font-weight: bold;">;;</span>
		<span style="color: #c20cb9; font-weight: bold;">kill</span><span style="color: #000000; font-weight: bold;">|</span>-<span style="color: #000000;">9</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">SIGNAL</span>=<span style="color: #ff0000;">'KILL'</span><span style="color: #000000; font-weight: bold;">;;</span>
		hup<span style="color: #000000; font-weight: bold;">|</span>-<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">SIGNAL</span>=<span style="color: #ff0000;">'HUP'</span><span style="color: #000000; font-weight: bold;">;;</span>
	<span style="color: #000000; font-weight: bold;">esac</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$SIGNAL</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</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;">shift</span>
	<span style="color: #000000; font-weight: bold;">else</span>
		<span style="color: #007800;">SIGNAL</span>=<span style="color: #ff0000;">'SHOOT'</span>
	<span style="color: #000000; font-weight: bold;">fi</span>
	<span style="color: #007800;">PROCS</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ps</span> ax <span style="color: #660033;">-eo</span> pid,<span style="color: #c20cb9; font-weight: bold;">comm</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">fgrep</span> <span style="color: #ff0000;">&quot;$1&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
	<span style="color: #007800;">MODE</span>=<span style="color: #000000;">0</span>
	<span style="color: #000000; font-weight: bold;">for</span> DATA <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$PROCS</span>; <span style="color: #000000; font-weight: bold;">do</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$MODE</span>&quot;</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
			<span style="color: #007800;">PID</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$DATA</span>&quot;</span>
			<span style="color: #007800;">MODE</span>=<span style="color: #000000;">1</span>
		<span style="color: #000000; font-weight: bold;">else</span>
			<span style="color: #007800;">CMD</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$DATA</span>&quot;</span>
			<span style="color: #007800;">MODE</span>=<span style="color: #000000;">0</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$SIGNAL</span>&quot;</span> == <span style="color: #ff0000;">'SHOOT'</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: #660033;">-n</span> <span style="color: #ff0000;">&quot;Shooting #<span style="color: #007800;">$PID</span> - <span style="color: #007800;">$CMD</span>...&quot;</span>
				<span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #007800;">$PID</span>
				<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>
				<span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #660033;">-9</span> <span style="color: #007800;">$PID</span>
				<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Shot&quot;</span>
			<span style="color: #000000; font-weight: bold;">else</span>
				<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Killing #<span style="color: #007800;">$PID</span> - <span style="color: #007800;">$CMD</span>...&quot;</span>
				<span style="color: #c20cb9; font-weight: bold;">kill</span> <span style="color: #007800;">$SIGNAL</span> <span style="color: #007800;">$PID</span>
				<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Killed&quot;</span>
			<span style="color: #000000; font-weight: bold;">fi</span>
		<span style="color: #000000; font-weight: bold;">fi</span>
	<span style="color: #000000; font-weight: bold;">done</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>To install it simply dump the above text inside your existing ~/.bashrc file.</p>
<p>Usage is quite simple:</p>
<p>To politely kill all processes (then force-killing if it still does not die) containing the string &#8216;gnome&#8217;:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p89code29'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8929"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p89code29"><pre class="bash" style="font-family:monospace;">zap gnome</pre></td></tr></table></div>

<p>To just kill-forcefully:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p89code30'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8930"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p89code30"><pre class="bash" style="font-family:monospace;">zap <span style="color: #660033;">-9</span> gnome</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://hash-bang.net/2009/01/zapping-processes-in-bash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

