<?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>Es Tea Double Eye &#187; bash</title>
	<atom:link href="http://stii.co.za/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://stii.co.za</link>
	<description>You&#039;re never too old for a happy childhood</description>
	<lastBuildDate>Fri, 19 Aug 2011 02:20:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<atom:link rel='hub' href='http://stii.co.za/?pushpress=hub'/>
		<item>
		<title>5 simple Bash tips</title>
		<link>http://stii.co.za/linux/5-simple-bash-tips/</link>
		<comments>http://stii.co.za/linux/5-simple-bash-tips/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 08:31:59 +0000</pubDate>
		<dc:creator>Stii</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[linux-commands]]></category>
		<category><![CDATA[tips and tricks]]></category>

		<guid isPermaLink="false">http://stii.co.za/?p=540</guid>
		<description><![CDATA[If you work in the command line on Bash a lot you&#8217;ll probably find that you often repeat commands. Often you need to run similar commands, but change something like a path, for example. Below are 5 very basic and simple things you should get into the habit of using as they just might make [...]]]></description>
			<content:encoded><![CDATA[<p>If you work in the command line on <a href="http://stii.co.za/tag/bash/">Bash</a> a lot you&#8217;ll probably find that you often repeat commands. Often you need to run similar commands, but change something like a path, for example. Below are 5 very basic and simple things you should get into the habit of using as they just might make your life simpler. They work wonders for me :)</p>
<p><strong>1. Search the command history:</strong></p>
<pre>
$ ctrl+r
(reverse-i-search)`ls': ls - l /opt/local/var/db/mysql5
</pre>
<p>This is by far the handiest tip. On the command line, press the Control button and R. This will activate a reverse interactive search. start typing something that was in the command and it will return the last command with the text in it. If it is not this particular command you were looking for, press <strong>Control+R</strong> again and it will return the next command. Keep hitting Control+R until you find the command you were looking for and hit enter to run the command. It beats retyping commands or pressing the up arrow until you get the command. :)</p>
<p><strong>2. Edit the previous command</strong></p>
<pre>
$ fc
</pre>
<p>Running the <strong>fc</strong> command opens your default system editor (<strong>VIM</strong> in most cases) and allow you to edit the command. After your changes when you exit VIM your edited command will execute. This is handy if you were running a complex long command but made a mistake and need to edit some things somewhere in the middle of the complex command. Handy if you remember you can do that. </p>
<p><strong>3. Run the last command of a certain type</strong></p>
<pre>
$ !ssh
ssh -l stii stii.co.za
</pre>
<p>If you&#8217;ve ran a bunch of commands and cannot remember exactly when last you ran a certain command, but you know you want to run the last ssh command, use the <strong>!ssh</strong> to run the last ssh command you&#8217;ve executed. This will save you time again on paging up through your history to find that command.</p>
<p><strong>4. Run the last command of a certain type, replacing some values</strong></p>
<pre>
$ !ssh:s/stii.co.za/afrigator.com
ssh -l stii afrigator.com
</pre>
<p>Same as the previous command except it will replace the domain name stii.co.za with the domain afrigator.com. Very, very handy one for running repetitive commands with different arguments. It sure as hell beats finding the command, backspacing the argument you want to change and typing in the correct value!</p>
<p><strong>5. Swop two characters quickly</strong></p>
<p>If you have twiddle fingers or cronic dyslexia like some of us, then this one&#8217;s a gem. ;) Lets say you typed a command and swapped two characters around:</p>
<pre>
$ cd /home/afrigtaor
</pre>
<p>Instead on moving your cursor to the a, backspacing twice and retyping the a and t in the correct order, all you need to do is to move your cursor to the a and press <strong>Control+t</strong> and it will swop the t and the a around fixing your typo. Although this one is a bit of a useless one, if you do swap characters often, it might be handy to know of this trick ;)</p>
<p>These tips are very basic and would probably not be news for most experienced <strong>Bash</strong> aficionados. I find these very handy and use them often on a daily basis, so maybe it could be handy to someone else. If you have a set of your own favorite tricks, please do let me know as I love learning and improving! <strong><em>Happy Bashing!</em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://stii.co.za/linux/5-simple-bash-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Bash to update Twitter with the current playing track in iTunes</title>
		<link>http://stii.co.za/hacking/bash-update-twitter-with-current-playing-track-in-itunes/</link>
		<comments>http://stii.co.za/hacking/bash-update-twitter-with-current-playing-track-in-itunes/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 15:15:53 +0000</pubDate>
		<dc:creator>Stii</dc:creator>
				<category><![CDATA[Hacking]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[itunes]]></category>
		<category><![CDATA[Music]]></category>
		<category><![CDATA[osascript]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://stii.co.za/?p=489</guid>
		<description><![CDATA[Bash can do everything. From curing cancer right up to making you look cool :) I wanted to update Twitter when I listen to something very, very cool like Tom Waits. This Bash script gets the info of the current playing track from iTunes using the osascript command (osascript executes AppleScript, so naturally, this is [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://stii.co.za/tag/bash">Bash</a> can do everything. From <a href="http://bashcurescancer.com/">curing cancer</a> right up to making you look <strong>cool</strong> :) I wanted to update <a href="http://stii.co.za/tag/twitter">Twitter</a> when I listen to something very, very cool like <a href="http://www.last.fm/music/Tom+Waits" rel="nofollow">Tom Waits</a>.</p>
<p>This Bash script gets the info of the current playing track from iTunes using the <strong>osascript command</strong> (osascript executes <strong>AppleScript</strong>, so naturally, this is for <strong>OSX</strong> ;-) ) and tweets it using <strong>curl</strong>. I had to use <strong>Perl</strong> to encode the update string since characters like &#038; was not behaving kind! :) Here it is:</p>
<pre>
#!/bin/sh

state=`osascript -e \
'tell application "iTunes" to player state as string'`;
if [ $state = "playing" ]; then
        artist=`osascript -e \
        'tell application "iTunes" to \
        artist of current track as string'`;
        track=`osascript -e \
        'tell application "iTunes" to \
        name of current track as string'`;
        album=`osascript -e \
       'tell application "iTunes" to \
       album of current track as string'`;
        msg="Listening to $track by $artist from $album";
        curl --basic --user "username:password" \
        --data-ascii "status=`perl -MURI::Escape -e \
        "print uri_escape('$msg');"`" \

http://twitter.com/statuses/update.json

fi
</pre>
<p>I saved it in a script called <strong>tweettune</strong> and </p>
<pre>
chmod 755 tweettune
</pre>
<p>to make it executable. If you run the script it will return the update in JSON format. </p>
<p>It works great. If you are cooler and your Bash is hotter than mine, please send/leave me the code to try! </p>
<p>PS: remember to replace username:password with your Twitter login credentials.<br />
PPS: Yes, I know <strong>OAuth</strong> for Twitter is out. This is not about using OAuth, but <strong>updating your Twitter status with the current track playing in iTunes</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://stii.co.za/hacking/bash-update-twitter-with-current-playing-track-in-itunes/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to calculate the days between two dates using Python. Quickly.</title>
		<link>http://stii.co.za/python/how-to-calculate-the-days-between-two-dates-using-python-quickly/</link>
		<comments>http://stii.co.za/python/how-to-calculate-the-days-between-two-dates-using-python-quickly/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 10:56:38 +0000</pubDate>
		<dc:creator>Stii</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[softwaredevelopment]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://stii.co.za/?p=331</guid>
		<description><![CDATA[This is one of those pretty useless things, but you never know when you might just have the need for something like this&#8230; I have not taken the time to investigate a quicker way using Bash, but my love of Python made me use it without even thinking twice! :) I first fired up the [...]]]></description>
			<content:encoded><![CDATA[<p>This is one of those pretty useless things, but you never know when you might just have the need for something like this&#8230; I have not taken the time to investigate a quicker way using <a href="http://stii.co.za/tag/bash/">Bash</a>, but my love of <a href="http://stii.co.za/tag/python/">Python</a> made me use it without even thinking twice! :)</p>
<p>I first fired up the interactive <strong>Python</strong> interpreter by simply typing the command python. Next, I imported the <strong>datetime module</strong>. Did a simple <strong>timedelta</strong> between two <strong>datetime objects</strong> and Jack&#8217;s your uncle.</p>
<pre>
$ python
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license"
for more information.
>>> import datetime
>>> print (datetime.date(2009, 03, 31) \
... - datetime.date(2009, 02, 06)).days
53
>>>
</pre>
<p><strong>Python</strong> not only is an excellent programming language, it is also a brilliant general purpose toolbox! <strong>Bash/Awk/whatever</strong> experts, is there a quicker way maybe? Would love to know. </p>
]]></content:encoded>
			<wfw:commentRss>http://stii.co.za/python/how-to-calculate-the-days-between-two-dates-using-python-quickly/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Bash script or command to get a process id</title>
		<link>http://stii.co.za/linux/bash-script-or-command-to-get-a-process-id/</link>
		<comments>http://stii.co.za/linux/bash-script-or-command-to-get-a-process-id/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 05:00:38 +0000</pubDate>
		<dc:creator>Stii</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux-commands]]></category>
		<category><![CDATA[shell-scripts]]></category>

		<guid isPermaLink="false">http://stii.co.za/?p=269</guid>
		<description><![CDATA[Bash (a.k.a. Bourne Again SHell) is one of the best tools you&#8217;ll ever come across on Linux, Unix or OSX. You can do virtually anything with it! It is said that bash even cures cancer! Now, let me make this very, very clear&#8230; I&#8217;m not an expert. I haven&#8217;t written much bash scripts for the [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://stii.co.za/wp-content/uploads/2009/01/gnu-head-sm.jpg" alt="bash logo" title="bash logo" width="129" height="122" class="alignright size-full wp-image-271" /><strong>Bash</strong> (a.k.a. <strong>B</strong>ourne <strong>A</strong>gain <strong>SH</strong>ell) is one of the best <strong>tools</strong> you&#8217;ll ever come across on <strong>Linux, Unix or OSX</strong>. You can do virtually anything with it! It is said that <a href="http://bashcurescancer.com/">bash even cures cancer</a>! Now, let me make this very, very clear&#8230; I&#8217;m not an <a href="http://singe.za.net/blog/archives/967-WorkTime-Script-to-Prevent-Innapropriate-Web-Surfing.html">expert</a>. I haven&#8217;t written much <strong>bash scripts</strong> for the past 5 years, so it would be fair to say I&#8217;m a <strong>n00b</strong>. At the very least, I&#8217;m not a <em>l33t bash afiçionado</em>.</p>
<p>I had a specific need. I needed a way to find out if a <strong>process</strong> was running and to obtain its <strong>process id (pid)</strong>. Well, in all honesty, I didn&#8217;t REALLY needed the <strong>pid</strong>, but thought it could maybe be handy for some, so I included it anyway. In my case, all I wanted to know was whether a process was running and if not, to start it. I digress. </p>
<p>First what I needed was to find the <strong>process</strong> I&#8217;m looking for:</p>
<pre>
#!/bin/bash

pid=`ps -eo pid,args | grep myprocess | \
grep -v grep | cut -c1-6`
#do what I need with the pid
</pre>
<p>What we did here:</p>
<p>First we get the process with the <strong>ps command</strong>. the <strong>-e</strong> option tells it to find processes in the entire environment. Without <strong>-e</strong> it will return only the processes owned by the user who runs the command. The <strong>-o</strong> option lets you specify which <strong>fields</strong> you want. That is exactly what the <strong>pid,args</strong> are. It is the fields we want, and the only fields we want in this instance.</p>
<p>Next we pipe the <strong>ps command</strong> through the <strong>grep command</strong>. <strong>grep</strong> extracts all the lines the <strong>ps command</strong> return that contains the string you specify, in this case &#8220;myprocess&#8221;.</p>
<p>Since the <strong>grep command</strong> will also return the <strong>grep process</strong>, we need to pipe it to another grep command to remove the grep proccess itself. The <strong>-v</strong> option tells grep to do an invert match, i.o.w. exclude the line containing the word &#8220;grep&#8221;. Lets look at an example:</p>
<p>If we only use:</p>
<pre>
$ ps -eo pid,args | grep myprocess
1234    /path/to/myprocess.sh
34761  grep myprocess
</pre>
<p>It returns two lines. We&#8217;re not interested in the second line and need to exclude it so the command:</p>
<pre>
$ ps -eo pid,args | grep myprocess | grep -v grep
1234   /path/to/myprocess.sh
</pre>
<p><strong>grep -v grep</strong> excludes the line that contains &#8220;grep&#8221;. Nifty. Just what we need.</p>
<p>The next step is to get the pid (process id) piping the results to the <strong>cut command</strong>. The <strong>-c1-6</strong> simply says &#8220;cut the characters from the first character up to and including the 6th character&#8221;.</p>
<pre>
... cut -c1-6
</pre>
<p>There you have it! You can now do what you need to with that process id. Crush it, Kill it, Destroy it, whatever tickles your fancy!</p>
<p>I&#8217;d like to know from the <strong>real experts</strong>, what would be an easier way to do it?  More important, what would be a better way to do it? I have to be honest, I didn&#8217;t really <strong>googled</strong> much for an answer since I was busy doing the <a href="http://afrigator.com">Afrigator</a> language files and was looking for anything to procrastinate on and this seemed like great fun! My solution might not be very good. For example, it does not allow for more than one process. It assumes that there would always only be one process running. It should be easy enough to allow for more than one process, but I&#8217;ll leave that up to you! You would need to split the lines&#8230; argh, just figure it out!</p>
<p>PS: <a href="http://justinhartman.com">Justin</a> &#038; <a href="http://lesterhein.blogspot.com">Lester</a>, next time you give me grief, I will replace the two of you with <strong>bash scripts</strong>!</p>
<h3>Update:</h3>
<p>Here is a simpler command by <a href="http://www.ddj.co.za/">Donald Jackson</a> via Twitter utilizing <strong>awk</strong>: </p>
<pre>
$ ps aux | grep processname | grep -v grep | awk '{print $2}'
</pre>
<p>Thanx <a href="http://twitter.com/donaldza">@donaldza</a></p>
]]></content:encoded>
			<wfw:commentRss>http://stii.co.za/linux/bash-script-or-command-to-get-a-process-id/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

