<?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>Personal blog of Chris Ergatides &#187; scripting</title>
	<atom:link href="http://blog.ergatides.com/tag/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ergatides.com</link>
	<description>jQuery, Linux, Technology, Web Design and random stuff.</description>
	<lastBuildDate>Mon, 06 Feb 2012 05:01:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Ternary operator for bash scripting.</title>
		<link>http://blog.ergatides.com/2012/01/13/ternary-operator-for-bash-scripting/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=ternary-operator-for-bash-scripting</link>
		<comments>http://blog.ergatides.com/2012/01/13/ternary-operator-for-bash-scripting/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 13:03:55 +0000</pubDate>
		<dc:creator>Chris Ergatides</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[check]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mounted]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[ternary]]></category>

		<guid isPermaLink="false">http://blog.ergatides.com/?p=1633</guid>


		<description><![CDATA[<p>I was curious whether or not <a href="http://en.wikipedia.org/wiki/Ternary_operation" target="_blank">ternary operations</a> are possible in bash or not. It turns out <a href="http://developmentality.wordpress.com/2010/10/20/ternary-operator-in-bash/" target="_blank">they are</a> but only for numeric comparisons. So what if you wanted to compare whether a string is empty if you cannot use a string comparison (-Z or -n for example)? Use the <a href="http://unstableme.blogspot.com/2008/03/find-string-length-bash.html" target="_blank">string&#8217;s length</a>.</p>
<p>As always with Linux, there&#8217;s a plethora of ways to do what I am doing below, so this is just an example for your reference:</p>
<pre class="brush: bash; title: ; notranslate">
function check_mounted () {
    local result=`df | grep $1`
    echo $((${#result} &gt; 0 ? 1 : 0))
}
</pre>
<p>Related posts:</p><ol>
<li><a href='http://blog.ergatides.com/2012/01/13/relative-path-to-absolute-path-in-a-bash-shell/' rel='bookmark' title='Relative path to absolute path in a bash shell.'>Relative path to absolute path in a bash shell.</a></li>
<li><a href='http://blog.ergatides.com/2010/02/24/handy-command-line-currency-converter/' rel='bookmark' title='Handy command line currency converter.'>Handy command line currency converter.</a></li>
<li><a href='http://blog.ergatides.com/2011/05/17/masking-fixed-length-strings-in-php/' rel='bookmark' title='Masking fixed length strings in PHP.'>Masking fixed length strings in PHP.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>I was curious whether or not <a href="http://en.wikipedia.org/wiki/Ternary_operation" target="_blank">ternary operations</a> are possible in bash or not. It turns out <a href="http://developmentality.wordpress.com/2010/10/20/ternary-operator-in-bash/" target="_blank">they are</a> but only for numeric comparisons. So what if you wanted to compare whether a string is empty if you cannot use a string comparison (-Z or -n for example)? Use the <a href="http://unstableme.blogspot.com/2008/03/find-string-length-bash.html" target="_blank">string&#8217;s length</a>.</p>
<p>As always with Linux, there&#8217;s a plethora of ways to do what I am doing below, so this is just an example for your reference:</p>
<pre class="brush: bash; title: ; notranslate">
function check_mounted () {
    local result=`df | grep $1`
    echo $((${#result} &gt; 0 ? 1 : 0))
}
</pre>
<p>Related posts:</p><ol>
<li><a href='http://blog.ergatides.com/2012/01/13/relative-path-to-absolute-path-in-a-bash-shell/' rel='bookmark' title='Relative path to absolute path in a bash shell.'>Relative path to absolute path in a bash shell.</a></li>
<li><a href='http://blog.ergatides.com/2010/02/24/handy-command-line-currency-converter/' rel='bookmark' title='Handy command line currency converter.'>Handy command line currency converter.</a></li>
<li><a href='http://blog.ergatides.com/2011/05/17/masking-fixed-length-strings-in-php/' rel='bookmark' title='Masking fixed length strings in PHP.'>Masking fixed length strings in PHP.</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.ergatides.com/2012/01/13/ternary-operator-for-bash-scripting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relative path to absolute path in a bash shell.</title>
		<link>http://blog.ergatides.com/2012/01/13/relative-path-to-absolute-path-in-a-bash-shell/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=relative-path-to-absolute-path-in-a-bash-shell</link>
		<comments>http://blog.ergatides.com/2012/01/13/relative-path-to-absolute-path-in-a-bash-shell/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 07:45:13 +0000</pubDate>
		<dc:creator>Chris Ergatides</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[dbpath]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[readlink]]></category>
		<category><![CDATA[realpath]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[skype]]></category>

		<guid isPermaLink="false">http://blog.ergatides.com/?p=1619</guid>


		<description><![CDATA[<p>There are many ways to convert a relative path to an absolute path which can be accessed regardless of the current working directory, particularly useful for scripting. Later, I may make a post to demonstrate the various ways, but the easiest way to do this is to use the <em>readlink</em> utility which comes bundled with just about every Linux distribution with the exception of Debian based systems which use <em>realpath</em> instead.</p>
<pre class="brush: bash; title: ; notranslate">
$ pwd
/media/usbdisk/bin

$ readlink -f ./../mnt/.Skype
/media/usbdisk/mnt/.Skype
</pre>
<p>When you use readlink in a script and assign a resulting path to a variable, you should add the n flag (-fn) to suppress the trailing newline.</p>
<p>As you might have noticed, I needed to use this in a script to launch Skype but specify that I want to use the data files on my usb key as opposed to the default ~/.Skype location. This however was not working with relative paths and Skype would crash with something like:</p>
<blockquote><p>$ FatalAssert(*out_ptr != &#8216;.&#8217; &#038;&#038; !strstr(out_ptr, &#8220;../&#8221;) &#038;&#038; !strstr(out_ptr, &#8220;..\\&#8221;))<br />
FatalAssert(*out_ptr != &#8216;.&#8217; &#038;&#038; !strstr(out_ptr, &#8220;../&#8221;) &#038;&#038; !strstr(out_ptr, &#8220;..\\&#8221;))</p></blockquote>
<p>The moment I pass a real path to Skype&#8217;s &#8211;dbpath parameter, everything works as expected.</p>
<p>Related posts:</p><ol>
<li><a href='http://blog.ergatides.com/2012/01/13/ternary-operator-for-bash-scripting/' rel='bookmark' title='Ternary operator for bash scripting.'>Ternary operator for bash scripting.</a></li>
<li><a href='http://blog.ergatides.com/2011/06/14/automatically-download-and-merge-multiple-pdf-files-with-a-unix-shell/' rel='bookmark' title='Automatically download and merge multiple PDF files with a Unix shell.'>Automatically download and merge multiple PDF files with a Unix shell.</a></li>
<li><a href='http://blog.ergatides.com/2011/11/18/how-to-install-kde-on-fedora-core-13-and-onwards/' rel='bookmark' title='How to install KDE on Fedora Core 13 and onwards.'>How to install KDE on Fedora Core 13 and onwards.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>There are many ways to convert a relative path to an absolute path which can be accessed regardless of the current working directory, particularly useful for scripting. Later, I may make a post to demonstrate the various ways, but the easiest way to do this is to use the <em>readlink</em> utility which comes bundled with just about every Linux distribution with the exception of Debian based systems which use <em>realpath</em> instead.</p>
<pre class="brush: bash; title: ; notranslate">
$ pwd
/media/usbdisk/bin

$ readlink -f ./../mnt/.Skype
/media/usbdisk/mnt/.Skype
</pre>
<p>When you use readlink in a script and assign a resulting path to a variable, you should add the n flag (-fn) to suppress the trailing newline.</p>
<p>As you might have noticed, I needed to use this in a script to launch Skype but specify that I want to use the data files on my usb key as opposed to the default ~/.Skype location. This however was not working with relative paths and Skype would crash with something like:</p>
<blockquote><p>$ FatalAssert(*out_ptr != &#8216;.&#8217; &#038;&#038; !strstr(out_ptr, &#8220;../&#8221;) &#038;&#038; !strstr(out_ptr, &#8220;..\\&#8221;))<br />
FatalAssert(*out_ptr != &#8216;.&#8217; &#038;&#038; !strstr(out_ptr, &#8220;../&#8221;) &#038;&#038; !strstr(out_ptr, &#8220;..\\&#8221;))</p></blockquote>
<p>The moment I pass a real path to Skype&#8217;s &#8211;dbpath parameter, everything works as expected.</p>
<p>Related posts:</p><ol>
<li><a href='http://blog.ergatides.com/2012/01/13/ternary-operator-for-bash-scripting/' rel='bookmark' title='Ternary operator for bash scripting.'>Ternary operator for bash scripting.</a></li>
<li><a href='http://blog.ergatides.com/2011/06/14/automatically-download-and-merge-multiple-pdf-files-with-a-unix-shell/' rel='bookmark' title='Automatically download and merge multiple PDF files with a Unix shell.'>Automatically download and merge multiple PDF files with a Unix shell.</a></li>
<li><a href='http://blog.ergatides.com/2011/11/18/how-to-install-kde-on-fedora-core-13-and-onwards/' rel='bookmark' title='How to install KDE on Fedora Core 13 and onwards.'>How to install KDE on Fedora Core 13 and onwards.</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.ergatides.com/2012/01/13/relative-path-to-absolute-path-in-a-bash-shell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Handy command line currency converter.</title>
		<link>http://blog.ergatides.com/2010/02/24/handy-command-line-currency-converter/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=handy-command-line-currency-converter</link>
		<comments>http://blog.ergatides.com/2010/02/24/handy-command-line-currency-converter/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 21:15:18 +0000</pubDate>
		<dc:creator>Chris Ergatides</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://blog.ergatides.com/?p=154</guid>


		<description><![CDATA[<p>In 2003, I wrote a custom Amazon-like shopping cart script. Back then, my web scraping skills weren&#8217;t, ahem, the best they could have been. For the site&#8217;s currency conversions, I had a cron job pull down 3 sets of converstions, calculated the average for each currency pair, then stored the result locally for my use.</p>
<p>At the time, I did this hoping that in a worse-case-scenario, a maximum of 2 sources would die in any weekend. Luckily, the sources lasted for the 3 years whilst I was working for that company (and apparently for another two after that). Now, I hear, they are manually updating a static text file&#8230; once in a while.</p>
<p>Today, I have no such needs but as always I am constantly looking for new ways to do things, should the need arise. I also needed a simple currency converter for doing small calculations like how much to top up a prepaid credit card by to purchase something from eBay which is listed in another currency.</p>
<p>Full credits to <a href="http://commandliners.com/2009/02/currency-conversion-script/">rafacas at commandliners</a> for the inspiration and as he points out, note that the script uses <a href="http://www.google.com/finance">Google Finance’s page</a>.</p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/bash

# 2009 - Rafa Casado - http://bit.ly/bYZwex
# 2010 - Chris Ergatides - http://bit.ly/9XjAUz

if [ $# -lt 2 ]; then
    echo -e &quot;\nUsage: $0 currency1 currency2 amount&quot;
    echo &quot;Default: $0 GBP EUR 1&quot;
    echo &quot;Example 1: $0 USD GBP&quot;
    echo &quot;Example 2: $0 GBP USD 42&quot;
fi

toUpper() {
    echo $@ | tr &quot;[:lower:]&quot; &quot;[:upper:]&quot;
}

if [ -n &quot;$1&quot; ]; then FROM=$(toUpper &quot;$1&quot;); else FROM=GBP; fi
if [ -n &quot;$2&quot; ]; then TO=$(toUpper &quot;$2&quot;); else TO=EUR; fi
if [ $TO == $FROM ]; then echo 'Nothing to do!'; exit 2; fi
if [ -n &quot;$3&quot; ]; then A=$3; else A=1; fi

CONVERTER=&quot;http://www.google.com/finance/converter?a=$A&amp;from=$FROM&amp;to=$TO&quot;

RESULT=`wget -nv -O - &quot;$CONVERTER&quot; 2&gt;&amp;1 | \
sed -n -e 's/.*&lt;span class=bld&gt;\(.*\)&lt;\/span&gt;.*/\1/p'`

echo -e &quot;\nResult: $A $FROM = $RESULT\n&quot;
</pre>
<p>So, let&#8217;s say you&#8217;ve named the script currency.sh and made it executable with chmod +x, what does the output look like?</p>
<pre class="brush: bash; title: ; notranslate">
#Called without any parameters:
[chris@r500 ~/bin]$ ./currency.sh

Usage: /home/chris/bin/currency.sh currency1 currency2 amount
Default: /home/chris/bin/currency.sh GBP EUR 1
Example 1: /home/chris/bin/currency.sh USD GBP
Example 2: /home/chris/bin/currency.sh GBP USD 42

Result: 1 GBP = 1.1387 EUR
</pre>
<pre class="brush: bash; title: ; notranslate">
#Called with example 1's parameters:
[chris@r500 ~/bin]$ ./currency.sh usd gbp

Result: 1 USD = 0.6475 GBP
</pre>
<pre class="brush: bash; title: ; notranslate">
#Called with example 2's parameters
[chris@r500 ~/bin]$ ./currency.sh gbp usd 42

Result: 42 GBP = 64.8606 USD
</pre>
<p>Related posts:</p><ol>
<li><a href='http://blog.ergatides.com/2011/02/21/whats-my-ip-check-your-ip-address-from-the-command-line/' rel='bookmark' title='What&#8217;s my IP? Check your IP address from the command line.'>What&#8217;s my IP? Check your IP address from the command line.</a></li>
<li><a href='http://blog.ergatides.com/2012/01/13/ternary-operator-for-bash-scripting/' rel='bookmark' title='Ternary operator for bash scripting.'>Ternary operator for bash scripting.</a></li>
<li><a href='http://blog.ergatides.com/2012/01/24/using-sed-to-search-and-replace-contents-of-next-line-in-a-file/' rel='bookmark' title='Using sed to search and replace contents of next line in a file.'>Using sed to search and replace contents of next line in a file.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In 2003, I wrote a custom Amazon-like shopping cart script. Back then, my web scraping skills weren&#8217;t, ahem, the best they could have been. For the site&#8217;s currency conversions, I had a cron job pull down 3 sets of converstions, calculated the average for each currency pair, then stored the result locally for my use.</p>
<p>At the time, I did this hoping that in a worse-case-scenario, a maximum of 2 sources would die in any weekend. Luckily, the sources lasted for the 3 years whilst I was working for that company (and apparently for another two after that). Now, I hear, they are manually updating a static text file&#8230; once in a while.</p>
<p>Today, I have no such needs but as always I am constantly looking for new ways to do things, should the need arise. I also needed a simple currency converter for doing small calculations like how much to top up a prepaid credit card by to purchase something from eBay which is listed in another currency.</p>
<p>Full credits to <a href="http://commandliners.com/2009/02/currency-conversion-script/">rafacas at commandliners</a> for the inspiration and as he points out, note that the script uses <a href="http://www.google.com/finance">Google Finance’s page</a>.</p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/bash

# 2009 - Rafa Casado - http://bit.ly/bYZwex
# 2010 - Chris Ergatides - http://bit.ly/9XjAUz

if [ $# -lt 2 ]; then
    echo -e &quot;\nUsage: $0 currency1 currency2 amount&quot;
    echo &quot;Default: $0 GBP EUR 1&quot;
    echo &quot;Example 1: $0 USD GBP&quot;
    echo &quot;Example 2: $0 GBP USD 42&quot;
fi

toUpper() {
    echo $@ | tr &quot;[:lower:]&quot; &quot;[:upper:]&quot;
}

if [ -n &quot;$1&quot; ]; then FROM=$(toUpper &quot;$1&quot;); else FROM=GBP; fi
if [ -n &quot;$2&quot; ]; then TO=$(toUpper &quot;$2&quot;); else TO=EUR; fi
if [ $TO == $FROM ]; then echo 'Nothing to do!'; exit 2; fi
if [ -n &quot;$3&quot; ]; then A=$3; else A=1; fi

CONVERTER=&quot;http://www.google.com/finance/converter?a=$A&amp;from=$FROM&amp;to=$TO&quot;

RESULT=`wget -nv -O - &quot;$CONVERTER&quot; 2&gt;&amp;1 | \
sed -n -e 's/.*&lt;span class=bld&gt;\(.*\)&lt;\/span&gt;.*/\1/p'`

echo -e &quot;\nResult: $A $FROM = $RESULT\n&quot;
</pre>
<p>So, let&#8217;s say you&#8217;ve named the script currency.sh and made it executable with chmod +x, what does the output look like?</p>
<pre class="brush: bash; title: ; notranslate">
#Called without any parameters:
[chris@r500 ~/bin]$ ./currency.sh

Usage: /home/chris/bin/currency.sh currency1 currency2 amount
Default: /home/chris/bin/currency.sh GBP EUR 1
Example 1: /home/chris/bin/currency.sh USD GBP
Example 2: /home/chris/bin/currency.sh GBP USD 42

Result: 1 GBP = 1.1387 EUR
</pre>
<pre class="brush: bash; title: ; notranslate">
#Called with example 1's parameters:
[chris@r500 ~/bin]$ ./currency.sh usd gbp

Result: 1 USD = 0.6475 GBP
</pre>
<pre class="brush: bash; title: ; notranslate">
#Called with example 2's parameters
[chris@r500 ~/bin]$ ./currency.sh gbp usd 42

Result: 42 GBP = 64.8606 USD
</pre>
<p>Related posts:</p><ol>
<li><a href='http://blog.ergatides.com/2011/02/21/whats-my-ip-check-your-ip-address-from-the-command-line/' rel='bookmark' title='What&#8217;s my IP? Check your IP address from the command line.'>What&#8217;s my IP? Check your IP address from the command line.</a></li>
<li><a href='http://blog.ergatides.com/2012/01/13/ternary-operator-for-bash-scripting/' rel='bookmark' title='Ternary operator for bash scripting.'>Ternary operator for bash scripting.</a></li>
<li><a href='http://blog.ergatides.com/2012/01/24/using-sed-to-search-and-replace-contents-of-next-line-in-a-file/' rel='bookmark' title='Using sed to search and replace contents of next line in a file.'>Using sed to search and replace contents of next line in a file.</a></li>
</ol>]]></content:encoded>
			<wfw:commentRss>http://blog.ergatides.com/2010/02/24/handy-command-line-currency-converter/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

