<?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>BigSmoke &#187; bash</title>
	<atom:link href="http://blog.bigsmoke.us/tag/bash/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.bigsmoke.us</link>
	<description>Smokes your problems, coughs fresh air.</description>
	<lastBuildDate>Sat, 04 Feb 2012 18:03:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Making a shell-script run with setuid root</title>
		<link>http://blog.bigsmoke.us/2011/02/03/suid-shell-script</link>
		<comments>http://blog.bigsmoke.us/2011/02/03/suid-shell-script#comments</comments>
		<pubDate>Wed, 02 Feb 2011 22:45:50 +0000</pubDate>
		<dc:creator>Rowan Rodrik</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[root]]></category>
		<category><![CDATA[setuid]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=1863</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>If you want to run a process with root privileges that you can invoke as a less unprivileged user, you can make the program setuid root. This can be very useful, for example, when you want a PHP or CGI script to call a backup process, or to create a new site or irrevocably delete you whole system. The latter example points to a serious security problem: if anyone can figure out a way to make your program do something you don&#8217;t want, you&#8217;re screwed, because you just gave them root privileges to wreak maximum havoc. That&#8217;s why, normally, scripts (anything executed by an interpreter by the kernel because of a shebang) won&#8217;t get elevated privileges when you set their setuid bit.</p>
<p>To understand the setuid bit, let&#8217;s first see what happens when I try to cat a file that belongs to root:</p>
<p><pre class="bash">su -
<span style="color: #808080; font-style: italic;"># I am now root; fear me</span>
touch no-one-can-touch-me
chmod <span style="color: #cc66cc;">600</span> no-one-can-touch-me
<span style="color: #000066;">exit</span>
cat no-one-can-touch-me
<span style="color: #808080; font-style: italic;"># cat: Permission denied </span></pre></p>
<p>Next, I&#8217;ll create a shell script that cats the file:</p>
<p><pre class="bash"><span style="color: #808080; font-style: italic;">#!/bin/bash</span>
&nbsp;
cat no-one-can-touch-me</pre></p>
<p>And make the script setuid root:</p>
<p><pre class="bash">su -
chown root:root script.sh
chmod +xs script.sh</pre></p>
<p>If I now execute the script, I still get the permission denied. What I need to make this work is a wrapper program. For that, I refer to Wiebe&#8217;s <a href="http://blog.bigsmoke.us/2011/02/02/executing-system-commands-from-php-with-suid-executable">post</a> about the same subject. (Yeah, I know: why bother publishing this if Wiebe already did an excellent job explaining? Well, I just hate to throw away an otherwise fine draft.)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2011/02/03/suid-shell-script/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remove appending slash from a path using Sed</title>
		<link>http://blog.bigsmoke.us/2011/01/29/sed-remove-appending-slash</link>
		<comments>http://blog.bigsmoke.us/2011/01/29/sed-remove-appending-slash#comments</comments>
		<pubDate>Sat, 29 Jan 2011 19:49:26 +0000</pubDate>
		<dc:creator>Rowan Rodrik</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=1849</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s how you can remove the appending slash from a path using sed, the <b>s</b>tream <b>ed</b>itor:</p>
<p><pre class="bash"><span style="color: #000066;">echo</span> /just/a/path/ | sed -e <span style="color: #ff0000;">'s#/$##'</span>
<span style="color: #808080; font-style: italic;"># Output: /just/a/path</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># And, if there isn't an appending slash, nothing happens:</span>
&nbsp;
<span style="color: #000066;">echo</span> /just/another/path | sed -e <span style="color: #ff0000;">'s#/$##'</span>
<span style="color: #808080; font-style: italic;"># Output: /just/another/path </span></pre></p>
<p>It works quite simple. Sed executes <em>expression</em> (<tt>-e</tt>) on its standard input. The expression is a substitution using regular expressions. The <tt>#</tt>-sign is the delimiter. The part (<tt>#/</tt>) between the first two hash signs is the matching expression and the (empty) part between the second and the third hash sign is the replacement expression. This expression (“<tt>s#/$##</tt>”) basically says: replace all occurrences of “<tt>/</tt>” at the end of the line (the dollar sign is the end-of-line anchor) with nothing.</p>
<p>To use this in a script is easy-peasy. Suppose <tt>$1</tt> is a system path that may or may not include an appending slash:</p>
<p><pre class="bash"><span style="color: #808080; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #0000ff;">sanitized_path=</span>`<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;$1&quot;</span> | sed -e <span style="color: #ff0000;">'s#/$##'</span>`
&nbsp;
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$sanitized_path</span></pre></p>
<p>This script outputs its first parameter with the appending slash removed.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2011/01/29/sed-remove-appending-slash/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash script template</title>
		<link>http://blog.bigsmoke.us/2011/01/05/bash-script-template</link>
		<comments>http://blog.bigsmoke.us/2011/01/05/bash-script-template#comments</comments>
		<pubDate>Wed, 05 Jan 2011 10:53:27 +0000</pubDate>
		<dc:creator>halfgaar</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=1804</guid>
		<description><![CDATA[A template bash script, for when you need something overengineerd that works.]]></description>
			<content:encoded><![CDATA[<p><pre class="bash"><span style="color: #808080; font-style: italic;">#!/bin/bash</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Author: Wiebe Cazemier (wiebe@halfgaar.net)</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Template bash script, for when you need something overengineerd :)</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Hack prevention</span>
<span style="color: #0000ff;">PATH=</span><span style="color: #ff0000;">&quot;/sbin:/usr/sbin:/bin:/usr/bin&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Error codes</span>
<span style="color: #0000ff;">wrong_params=</span><span style="color: #cc66cc;">5</span>
<span style="color: #0000ff;">interrupted=</span><span style="color: #cc66cc;">99</span>
<span style="color: #0000ff;">default_error=</span><span style="color: #cc66cc;">1</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Function to echo in color. Don't supply color for normal color.</span>
echo_color<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">message=</span><span style="color: #ff0000;">&quot;$1&quot;</span>
  <span style="color: #0000ff;">color=</span><span style="color: #ff0000;">&quot;$2&quot;</span>
&nbsp;
  <span style="color: #0000ff;">red_begin=</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\0</span>33[01;31m&quot;</span>
  <span style="color: #0000ff;">green_begin=</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\0</span>33[01;32m&quot;</span>
  <span style="color: #0000ff;">yellow_begin=</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\0</span>33[01;33m&quot;</span>
  <span style="color: #0000ff;">color_end=</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\0</span>33[00m&quot;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># Set color to normal when there is no color</span>
  <span style="color: #66cc66;">&#91;</span> ! <span style="color: #ff0000;">&quot;$color&quot;</span> <span style="color: #66cc66;">&#93;</span> &amp;&amp; <span style="color: #0000ff;">color_begin=</span><span style="color: #ff0000;">&quot;$color_end&quot;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">&quot;$color&quot;</span> == <span style="color: #ff0000;">&quot;red&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
    <span style="color: #0000ff;">color_begin=</span><span style="color: #ff0000;">&quot;$red_begin&quot;</span>
  <span style="color: #b1b100;">fi</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">&quot;$color&quot;</span> == <span style="color: #ff0000;">&quot;green&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
    <span style="color: #0000ff;">color_begin=</span><span style="color: #ff0000;">&quot;$green_begin&quot;</span>
  <span style="color: #b1b100;">fi</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">&quot;$color&quot;</span> == <span style="color: #ff0000;">&quot;yellow&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
    <span style="color: #0000ff;">color_begin=</span><span style="color: #ff0000;">&quot;$yellow_begin&quot;</span>
  <span style="color: #b1b100;">fi</span>
&nbsp;
  <span style="color: #000066;">echo</span> -e <span style="color: #ff0000;">&quot;${color_begin}${message}${color_end}&quot;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
end<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">message=</span><span style="color: #ff0000;">&quot;$1&quot;</span>
  <span style="color: #0000ff;">exit_status=</span><span style="color: #ff0000;">&quot;$2&quot;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -z <span style="color: #ff0000;">&quot;$exit_status&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
    <span style="color: #0000ff;">exit_status=</span><span style="color: #ff0000;">&quot;0&quot;</span>
  <span style="color: #b1b100;">fi</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> ! <span style="color: #ff0000;">&quot;$exit_status&quot;</span> -eq <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
    echo_color <span style="color: #ff0000;">&quot;$message&quot;</span> <span style="color: #ff0000;">&quot;red&quot;</span>
  <span style="color: #b1b100;">else</span>
    echo_color <span style="color: #ff0000;">&quot;$message&quot;</span> <span style="color: #ff0000;">&quot;green&quot;</span>
  <span style="color: #b1b100;">fi</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">&quot;$exit_status&quot;</span> -eq <span style="color: #ff0000;">&quot;$wrong_params&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
    dohelp
  <span style="color: #b1b100;">fi</span>
&nbsp;
  <span style="color: #000066;">exit</span> <span style="color: #0000ff;">$exit_status</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Define function to call when SIGTERM is received</span>
<span style="color: #000066;">trap</span> <span style="color: #ff0000;">&quot;end 'Interrupted' $interrupted&quot;</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">15</span>
&nbsp;
dohelp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>
  <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;Example script&quot;</span>
  <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>
  <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;help = todo&quot;</span>
 
  <span style="color: #808080; font-style: italic;"># Exit because you don't want the script to do anything after displaying help</span>
  <span style="color: #000066;">exit</span> 
<span style="color: #66cc66;">&#125;</span>
 
 
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#91;</span> -n <span style="color: #ff0000;">&quot;$*&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">do</span>
  <span style="color: #0000ff;">flag=</span>$<span style="color: #cc66cc;">1</span>
  <span style="color: #0000ff;">value=</span>$<span style="color: #cc66cc;">2</span>
 
  <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;$flag&quot;</span> <span style="color: #b1b100;">in</span>
    <span style="color: #ff0000;">&quot;--option1&quot;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #0000ff;">option1=</span><span style="color: #0000ff;">$value</span>
      <span style="color: #000066;">shift</span>
    ;;
    <span style="color: #ff0000;">&quot;--help&quot;</span><span style="color: #66cc66;">&#41;</span>
      dohelp
    ;;
    <span style="color: #ff0000;">&quot;--&quot;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #000066;">break</span>
    ;;
    *<span style="color: #66cc66;">&#41;</span>
      end <span style="color: #ff0000;">&quot;unknown option $flag. Type --help&quot;</span> <span style="color: #ff0000;">&quot;$wrong_params&quot;</span>
    ;;
  <span style="color: #b1b100;">esac</span>
 
  <span style="color: #000066;">shift</span>
<span style="color: #b1b100;">done</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -z <span style="color: #ff0000;">&quot;$option1&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
  end <span style="color: #ff0000;">&quot;option1 not given&quot;</span> <span style="color: #0000ff;">$wrong_params</span>
<span style="color: #b1b100;">fi</span></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2011/01/05/bash-script-template/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash parameter parsing</title>
		<link>http://blog.bigsmoke.us/2011/01/05/bash-parameter-parsing</link>
		<comments>http://blog.bigsmoke.us/2011/01/05/bash-parameter-parsing#comments</comments>
		<pubDate>Wed, 05 Jan 2011 10:20:29 +0000</pubDate>
		<dc:creator>halfgaar</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[options]]></category>
		<category><![CDATA[parameters]]></category>
		<category><![CDATA[params]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=1801</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>
Here is a code snippet I use for parameter parsing:
</p>
<p><pre class="bash">dohelp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;Example script&quot;</span>
  <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># Exit because you don't want the script to do anything after displaying help</span>
  <span style="color: #000066;">exit</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
 
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#91;</span> -n <span style="color: #ff0000;">&quot;$*&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">do</span>
  <span style="color: #0000ff;">flag=</span>$<span style="color: #cc66cc;">1</span>
  <span style="color: #0000ff;">value=</span>$<span style="color: #cc66cc;">2</span>
&nbsp;
  <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;$flag&quot;</span> <span style="color: #b1b100;">in</span>
    <span style="color: #ff0000;">&quot;--one&quot;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #0000ff;">one=</span><span style="color: #0000ff;">$value</span>
      <span style="color: #000066;">shift</span>
    ;;
    <span style="color: #ff0000;">&quot;--two&quot;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #0000ff;">two=</span><span style="color: #0000ff;">$value</span>
      <span style="color: #000066;">shift</span>
    ;;
    <span style="color: #ff0000;">&quot;--pretend&quot;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #0000ff;">pretend=</span>true
    ;;
    <span style="color: #ff0000;">&quot;--help&quot;</span><span style="color: #66cc66;">&#41;</span>
      dohelp
    ;;
    <span style="color: #ff0000;">&quot;--&quot;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #000066;">break</span>
    ;;
    *<span style="color: #66cc66;">&#41;</span>
      <span style="color: #000066;">echo</span> -e <span style="color: #ff0000;">&quot;unknown option $flag<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
      dohelp
    ;;
  <span style="color: #b1b100;">esac</span>
&nbsp;
  <span style="color: #000066;">shift</span>
<span style="color: #b1b100;">done</span></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2011/01/05/bash-parameter-parsing/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preventing syntax errors with old shell scripts</title>
		<link>http://blog.bigsmoke.us/2010/02/14/preventing-syntax-errors-with-old-shell-scripts</link>
		<comments>http://blog.bigsmoke.us/2010/02/14/preventing-syntax-errors-with-old-shell-scripts#comments</comments>
		<pubDate>Sun, 14 Feb 2010 15:45:21 +0000</pubDate>
		<dc:creator>halfgaar</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[unreal tournament]]></category>
		<category><![CDATA[ut]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=1257</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>
I was trying to install Unreal Tournament GOTY on one of my Linux machines. I downloaded and ran the script <a href="http://www.filefront.com/1634336/ut-install-436-GOTY.run">ut-install-436-GOTY.run</a> but I got this error:
</p>

<pre class="php">cannot open `<span style="color: #cc66cc;">+6</span><span style="color: #ff0000;">' for reading: No such file or directory </span></pre>

<p>
This line caused it:
</p>

<pre class="php">sum1=`tail <span style="color: #cc66cc;">+6</span> $<span style="color: #cc66cc;">0</span> | cksum | sed -e <span style="color: #ff0000;">'s/ /Z/'</span> -e <span style="color: #ff0000;">'s/   /Z/'</span> | cut -dZ -f1`</pre>

<p>
To fix it, I set this environment variable:
</p>

<pre class="php">export _POSIX2_VERSION=<span style="color: #cc66cc;">199209</span></pre>

<p>
Apparently, this makes programs behave differently. Research is required to find out exactly what it does&#8230;
</p>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2010/02/14/preventing-syntax-errors-with-old-shell-scripts/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Replacing the full contents of a Subversion working (sub)dir</title>
		<link>http://blog.bigsmoke.us/2010/01/14/svn-replace-dir</link>
		<comments>http://blog.bigsmoke.us/2010/01/14/svn-replace-dir#comments</comments>
		<pubDate>Wed, 13 Jan 2010 22:01:14 +0000</pubDate>
		<dc:creator>Rowan Rodrik</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[diff]]></category>
		<category><![CDATA[Subversion]]></category>
		<category><![CDATA[svn]]></category>
		<category><![CDATA[svn-replace-dir]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=1000</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>The annoyances that I suffered earlier today during the <a href="http://blog.bigsmoke.us/2008/03/13/upgrading-web-apps-with-diff-and-patch#comment-90523">upgrade of a WordPress plugin</a> made me turn to my <a href="http://blog.bigsmoke.us/tag/vim">favorite text-editor</a> to create a simple script, <tt><a href="http://gist.github.com/276574">svn-replace-dir</a></tt>:</p>

<pre class="bash"><span style="color: #808080; font-style: italic;">#!/bin/bash</span>
&nbsp;
usage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    cat &lt;&lt;<span style="color: #ff0000;">&quot;EOF&quot;</span>
$<span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#91;</span>--dry-run<span style="color: #66cc66;">&#93;</span> &lt;svn_dir&gt; &lt;replacement_dir&gt;
&nbsp;
This script replaces the contents of &lt;svn_dir&gt; with the contents of &lt;replacement_dir&gt;,
where &lt;replacement_dir&gt; is not an svn directory.
&nbsp;
Copyleft <span style="color: #cc66cc;">2010</span>, Rowan Rodrik van der Molen &lt;rowan@bigsmoke.us&gt;
EOF
<span style="color: #66cc66;">&#125;</span>
&nbsp;
fatal_error<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">message=</span>$<span style="color: #cc66cc;">1</span>
&nbsp;
    <span style="color: #000066;">echo</span> -e <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\e</span>[1;31m$message<span style="color: #000099; font-weight: bold;">\e</span>[0m&quot;</span>
    <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">1</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
usage_error<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">error=</span><span style="color: #ff0000;">&quot;Wrong usage.&quot;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -n <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
        <span style="color: #0000ff;">error=</span>$<span style="color: #cc66cc;">1</span>
    <span style="color: #b1b100;">fi</span>
&nbsp;
    <span style="color: #000066;">echo</span> -e <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\e</span>[1;31m$error<span style="color: #000099; font-weight: bold;">\e</span>[0m&quot;</span>
    <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">1</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
run_command<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066;">echo</span> -e <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\e</span>[1;34m$1<span style="color: #000099; font-weight: bold;">\e</span>[0m&quot;</span>
&nbsp;
    <span style="color: #66cc66;">&#91;</span> <span style="color: #0000ff;">$dry_run</span> == <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#93;</span> || <span style="color: #000066;">eval</span> $<span style="color: #cc66cc;">1</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">dry_run=</span><span style="color: #cc66cc;">0</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> $<span style="color: #cc66cc;">1</span> == <span style="color: #ff0000;">'--dry-run'</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
  <span style="color: #0000ff;">dry_run=</span><span style="color: #cc66cc;">1</span>
  <span style="color: #000066;">shift</span>
<span style="color: #b1b100;">fi</span>
&nbsp;
&nbsp;
<span style="color: #66cc66;">&#91;</span> $<span style="color: #808080; font-style: italic;"># == 2 ] || usage_error &quot;Wrong number of arguments.&quot;</span>
&nbsp;
<span style="color: #0000ff;">svn_dir=</span>`<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;$1&quot;</span>|sed -e <span style="color: #ff0000;">'s#/$##'</span>`
<span style="color: #0000ff;">replacement_dir=</span>`<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;$2&quot;</span>|sed -e <span style="color: #ff0000;">'s#/$##'</span>`
<span style="color: #0000ff;">begin_path=</span><span style="color: #0000ff;">$PWD</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#if [ &quot;${svn_dir:0:1}&quot; != &quot;/&quot; ]; then svn_dir=&quot;$PWD/$svn_dir&quot;; fi</span>
<span style="color: #808080; font-style: italic;">#if [ &quot;${replacement_dir:0:1}&quot; != &quot;/&quot; ]; then replacement_dir=&quot;$PWD/$replacement_dir&quot;; fi</span>
&nbsp;
<span style="color: #66cc66;">&#91;</span> -d <span style="color: #ff0000;">&quot;$svn_dir&quot;</span> <span style="color: #66cc66;">&#93;</span> || usage_error <span style="color: #ff0000;">&quot;$svn_dir is not a directory.&quot;</span>
<span style="color: #66cc66;">&#91;</span> -d <span style="color: #ff0000;">&quot;$replacement_dir&quot;</span> <span style="color: #66cc66;">&#93;</span> || usage_error <span style="color: #ff0000;">&quot;$replacement_dir is not a directory.&quot;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;"># Create all subdirectories in $svn_dir that do not yet exist</span>
<span style="color: #000066;">cd</span> <span style="color: #0000ff;">$replacement_dir</span>
find . -mindepth <span style="color: #cc66cc;">1</span> -<span style="color: #000066;">type</span> d -print | sed -e <span style="color: #ff0000;">'s#^./##'</span> | <span style="color: #b1b100;">while</span> <span style="color: #000066;">read</span> d; <span style="color: #b1b100;">do</span>
    <span style="color: #000066;">cd</span> <span style="color: #0000ff;">$begin_path</span>/<span style="color: #0000ff;">$svn_dir</span>
    <span style="color: #808080; font-style: italic;"># Doesn't the destination directory already exist?</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> ! -d <span style="color: #ff0000;">&quot;$d&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
        run_command <span style="color: #ff0000;">&quot;svn mkdir '$d'&quot;</span>
    <span style="color: #b1b100;">fi</span>
<span style="color: #b1b100;">done</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Copy all files from $replacement_dir to $svn_dir</span>
<span style="color: #000066;">cd</span> <span style="color: #0000ff;">$begin_path</span>/<span style="color: #0000ff;">$replacement_dir</span>
find . -<span style="color: #000066;">type</span> f -print | sed -e <span style="color: #ff0000;">'s#^./##'</span> | <span style="color: #b1b100;">while</span> <span style="color: #000066;">read</span> f; <span style="color: #b1b100;">do</span>
    <span style="color: #000066;">cd</span> <span style="color: #0000ff;">$begin_path</span>
    run_command <span style="color: #ff0000;">&quot;cp '$replacement_dir/$f' '$svn_dir/$f'&quot;</span> <span style="color: #808080; font-style: italic;"># FIXME: Quoting problem</span>
<span style="color: #b1b100;">done</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Remove all files that do no longer exist in $replacement dir</span>
<span style="color: #000066;">cd</span> <span style="color: #0000ff;">$begin_path</span>/<span style="color: #0000ff;">$svn_dir</span>
find . -<span style="color: #000066;">type</span> f -print | grep -v <span style="color: #ff0000;">'.svn'</span> | <span style="color: #b1b100;">while</span> <span style="color: #000066;">read</span> f; <span style="color: #b1b100;">do</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> ! -f <span style="color: #ff0000;">&quot;$begin_path/$replacement_dir/$f&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
        run_command <span style="color: #ff0000;">&quot;svn rm '$f'&quot;</span>
    <span style="color: #b1b100;">fi</span>
<span style="color: #b1b100;">done</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Remove all subdirs that do no longer exist in $replacement dir</span>
<span style="color: #000066;">cd</span> <span style="color: #0000ff;">$begin_path</span>/<span style="color: #0000ff;">$svn_dir</span>
find . -mindepth <span style="color: #cc66cc;">1</span> -<span style="color: #000066;">type</span> d -print | grep -v <span style="color: #ff0000;">'.svn'</span> | <span style="color: #b1b100;">while</span> <span style="color: #000066;">read</span> d; <span style="color: #b1b100;">do</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> ! -d <span style="color: #ff0000;">&quot;$begin_path/$replacement_dir/$d&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
        run_command <span style="color: #ff0000;">&quot;svn rm '$d'&quot;</span>
    <span style="color: #b1b100;">fi</span>
<span style="color: #b1b100;">done</span>
&nbsp;
<span style="color: #000066;">exit</span> <span style="color: #cc66cc;">0</span></pre>

<p>Using the script is simple:</p>

<pre class="bash">svn-replace-dir simple-tags new-simple-tags|less -R</pre>

<p>It replaces all the contents of the first directory (<q><tt>simple-tags</tt></q> in the example) with those of the second directory and it deletes everything that is no longer present in the second dir. In the process, it does all the necessary calls to <tt>svn mkdir</tt>, <tt>svn rm</tt> and (in the next version) <tt>svn add</tt>.</p>

<p><tt>diff</tt> tells me that the script has done its work correctly:</p>

<pre class="bash">diff -x .svn -ruN simple-tags new-simple-tags
<span style="color: #808080; font-style: italic;"># Emptiness is bliss :-) </span></pre>

<p>This is another one of these occasions when Git would have made life so much easier. Luckily, at least there&#8217;s GitHub to host this script as a Gist. <a href="http://gist.github.com/276574">Check there</a> if you want to fetch the newest version of this script.</p> ]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2010/01/14/svn-replace-dir/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My custom Linux environment</title>
		<link>http://blog.bigsmoke.us/2010/01/04/my-custom-linux-environment</link>
		<comments>http://blog.bigsmoke.us/2010/01/04/my-custom-linux-environment#comments</comments>
		<pubDate>Mon, 04 Jan 2010 09:54:07 +0000</pubDate>
		<dc:creator>halfgaar</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Screen]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=978</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>
On every machine that I install, I need a custom environment. At the very basic, I need screen and bash customizations. I will attempt to keep this blog post up-to-date with my most recent config.
</p>

<p>
/etc/bash.bashrc_halfgaar (naming scheme depends on distro):
</p>

<pre class="bash"><span style="color: #b1b100;">function</span> prompt_command <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066;">local</span> <span style="color: #0000ff;">XTERM_TITLE=</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\e</span>]2;<span style="color: #000099; font-weight: bold;">\u</span>@<span style="color: #000099; font-weight: bold;">\H</span>:<span style="color: #000099; font-weight: bold;">\w</span><span style="color: #000099; font-weight: bold;">\a</span>&quot;</span>
&nbsp;
  <span style="color: #000066;">local</span> <span style="color: #0000ff;">BGJOBS_COLOR=</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\[</span><span style="color: #000099; font-weight: bold;">\e</span>[1;30m<span style="color: #000099; font-weight: bold;">\]</span>&quot;</span>
  <span style="color: #000066;">local</span> <span style="color: #0000ff;">BGJOBS=</span><span style="color: #ff0000;">&quot;&quot;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">&quot;$(jobs | head -c1)&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span> <span style="color: #0000ff;">BGJOBS=</span><span style="color: #ff0000;">&quot; $BGJOBS_COLOR(bg:<span style="color: #000099; font-weight: bold;">\j</span>)&quot;</span>; <span style="color: #b1b100;">fi</span>
&nbsp;
  <span style="color: #000066;">local</span> <span style="color: #0000ff;">DOLLAR_COLOR=</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\[</span><span style="color: #000099; font-weight: bold;">\e</span>[1;32m<span style="color: #000099; font-weight: bold;">\]</span>&quot;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span> <span style="color: #0000ff;">$<span style="color: #66cc66;">&#123;</span>EUID<span style="color: #66cc66;">&#125;</span></span> == <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> ; <span style="color: #b1b100;">then</span> <span style="color: #0000ff;">DOLLAR_COLOR=</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\[</span><span style="color: #000099; font-weight: bold;">\e</span>[1;31m<span style="color: #000099; font-weight: bold;">\]</span>&quot;</span>; <span style="color: #b1b100;">fi</span>
  <span style="color: #000066;">local</span> <span style="color: #0000ff;">DOLLAR=</span><span style="color: #ff0000;">&quot;$DOLLAR_COLOR<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\$</span>&quot;</span>
&nbsp;
  <span style="color: #000066;">local</span> <span style="color: #0000ff;">USER_COLOR=</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\[</span><span style="color: #000099; font-weight: bold;">\e</span>[1;32m<span style="color: #000099; font-weight: bold;">\]</span>&quot;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span> <span style="color: #0000ff;">$<span style="color: #66cc66;">&#123;</span>EUID<span style="color: #66cc66;">&#125;</span></span> == <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span> <span style="color: #0000ff;">USER_COLOR=</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\[</span><span style="color: #000099; font-weight: bold;">\e</span>[41;1;32m<span style="color: #000099; font-weight: bold;">\]</span>&quot;</span>; <span style="color: #b1b100;">fi</span>
&nbsp;
  <span style="color: #0000ff;">PS1=</span><span style="color: #ff0000;">&quot;$XTERM_TITLE$USER_COLOR<span style="color: #000099; font-weight: bold;">\u</span><span style="color: #000099; font-weight: bold;">\[</span><span style="color: #000099; font-weight: bold;">\e</span>[1;32m<span style="color: #000099; font-weight: bold;">\]</span>@<span style="color: #000099; font-weight: bold;">\H</span>:<span style="color: #000099; font-weight: bold;">\[</span><span style="color: #000099; font-weight: bold;">\e</span>[m<span style="color: #000099; font-weight: bold;">\]</span> <span style="color: #000099; font-weight: bold;">\[</span><span style="color: #000099; font-weight: bold;">\e</span>[1;34m<span style="color: #000099; font-weight: bold;">\]</span><span style="color: #000099; font-weight: bold;">\w</span><span style="color: #000099; font-weight: bold;">\[</span><span style="color: #000099; font-weight: bold;">\e</span>[m<span style="color: #000099; font-weight: bold;">\]</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\</span>
$DOLLAR$BGJOBS <span style="color: #000099; font-weight: bold;">\[</span><span style="color: #000099; font-weight: bold;">\e</span>[m<span style="color: #000099; font-weight: bold;">\]</span>&quot;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000066;">export</span> <span style="color: #0000ff;">PROMPT_COMMAND=</span>prompt_command
&nbsp;
<span style="color: #000066;">export</span> <span style="color: #0000ff;">EDITOR=</span>vim
&nbsp;
<span style="color: #000066;">alias</span> <span style="color: #0000ff;">ls=</span><span style="color: #ff0000;">'ls --color=auto'</span>
<span style="color: #000066;">alias</span> <span style="color: #0000ff;">ll=</span><span style="color: #ff0000;">'ls -l'</span>
<span style="color: #000066;">alias</span> <span style="color: #0000ff;">lh=</span><span style="color: #ff0000;">'ls -lh'</span>
<span style="color: #000066;">alias</span> <span style="color: #0000ff;">grep=</span><span style="color: #ff0000;">'grep --color=auto'</span></pre>

<p>
Don&#8217;t forget to source the file in ~/.bashrc
</p>

<p>
~/.screenrc:
</p>

<pre class="php">caption always <span style="color: #ff0000;">&quot;%{= kB}%-Lw%{=s kB}%50&gt;%n%f* %t %{-}%+Lw%&lt;&quot;</span>
vbell off
startup_message off
term linux</pre>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2010/01/04/my-custom-linux-environment/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash script for sending SMS using Mollie</title>
		<link>http://blog.bigsmoke.us/2009/12/12/bash-script-for-sending-sms-using-mollie</link>
		<comments>http://blog.bigsmoke.us/2009/12/12/bash-script-for-sending-sms-using-mollie#comments</comments>
		<pubDate>Sat, 12 Dec 2009 12:35:32 +0000</pubDate>
		<dc:creator>halfgaar</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[mollie]]></category>
		<category><![CDATA[sms]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=909</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>
I signed up for a <a href="http://www.mollie.nl"/>Mollie account</a> so that I can send SMS&#8217;s from my machines. To do that, I needed a bash script, so I wrote one:
</p>

<pre class="bash"><span style="color: #808080; font-style: italic;">#! /bin/bash</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Script to send an SMS email notifcation to Mollie's HTTP gateways</span>
<span style="color: #808080; font-style: italic;"># Based on the SMS2Email script which can also be found on nagiosexchange.org</span>
<span style="color: #808080; font-style: italic;"># Reworked by Dennis Storm - Brainstorm ICT</span>
<span style="color: #808080; font-style: italic;"># Again reworked by Wiebe Cazemier (wiebe@halfgaar.net), to include proper failure checks</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;">#################################################################################</span>
 
<span style="color: #0000ff;">config_file=</span><span style="color: #ff0000;">&quot;/etc/send-sms.conf&quot;</span>
<span style="color: #0000ff;">log_file=</span><span style="color: #ff0000;">&quot;/var/log/send-sms.log&quot;</span>
 
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -L <span style="color: #ff0000;">&quot;$log_file&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
  <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;cannot continue, $log_file is a symlink.&quot;</span>
  <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">2</span>
<span style="color: #b1b100;">fi</span>
 
touch <span style="color: #ff0000;">&quot;$log_file&quot;</span> &gt; /dev/null <span style="color: #cc66cc;">2</span>&gt;&amp;<span style="color: #cc66cc;">1</span>
 
<span style="color: #0000ff;">sender=</span><span style="color: #ff0000;">&quot;SMSScript&quot;</span>
 
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -f <span style="color: #ff0000;">&quot;$config_file&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
  <span style="color: #000066;">source</span> <span style="color: #0000ff;">$config_file</span>
<span style="color: #b1b100;">fi</span>
 
<span style="color: #0000ff;">curl_location=</span>`which curl <span style="color: #cc66cc;">2</span>&gt; /dev/null`
 
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -z <span style="color: #ff0000;">&quot;$curl_location&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
  <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;Curl command not found. Install that.&quot;</span> &gt;&amp;<span style="color: #cc66cc;">2</span>
  <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">1</span>
<span style="color: #b1b100;">fi</span>
 
logfail<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">message=</span><span style="color: #ff0000;">&quot;$1&quot;</span>
  <span style="color: #0000ff;">message=</span><span style="color: #ff0000;">&quot;Failure: [`date`]: $message&quot;</span> 
 
  <span style="color: #000066;">echo</span> <span style="color: #0000ff;">$message</span>
 
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -w <span style="color: #ff0000;">&quot;$log_file&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;$message&quot;</span> &gt;&gt; <span style="color: #ff0000;">&quot;$log_file&quot;</span>
  <span style="color: #b1b100;">fi</span>
 
  <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">1</span>
<span style="color: #66cc66;">&#125;</span>
 
<span style="color: #808080; font-style: italic;"># Show usage if necessary</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> $<span style="color: #808080; font-style: italic;"># -eq 0 ]; then</span>
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;Usage: $0 -s [sender] -n [numbers] -m [message] -u [username] -p [password]&quot;</span>;
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>;
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;[numbers]  = SMS numbers (if multiple, enclose in quotes) to send message to.&quot;</span>;
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;[message]  = Text of message you want to send&quot;</span>;
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;[username] = Username assocated with Mollie  account&quot;</span>;
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;[sender]   = Sender&quot;</span>
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;[password] = MD5 HTTP API Password assocated with Mollie account&quot;</span>;
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;-d         = Dry run, pretend success.&quot;</span>
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;The numbers, sender, username and password options are optional and&quot;</span>;
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;override the account credentials defined in $config_file.&quot;</span>;
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;A log file $log_file will be kept if it is writable to the user.&quot;</span>
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span>;
    <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">1</span>;
<span style="color: #b1b100;">fi</span>
 
<span style="color: #808080; font-style: italic;"># Get command line arguments</span>
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">&quot;$1&quot;</span> != <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">&#93;</span> ; <span style="color: #b1b100;">do</span>
    <span style="color: #b1b100;">case</span> $<span style="color: #cc66cc;">1</span>
    <span style="color: #b1b100;">in</span>
  -n<span style="color: #66cc66;">&#41;</span>
      <span style="color: #808080; font-style: italic;"># Get the SMS numbers that we should send message to</span>
      <span style="color: #0000ff;">numbers=</span><span style="color: #ff0000;">&quot;$2&quot;</span>;
      <span style="color: #000066;">shift</span> <span style="color: #cc66cc;">2</span>;
      ;;
  -m<span style="color: #66cc66;">&#41;</span>
      <span style="color: #808080; font-style: italic;"># Get the message we should send</span>
      <span style="color: #0000ff;">message=</span><span style="color: #ff0000;">&quot;$2&quot;</span>;
      <span style="color: #000066;">shift</span> <span style="color: #cc66cc;">2</span>;
      ;;
  -s<span style="color: #66cc66;">&#41;</span>
      <span style="color: #808080; font-style: italic;"># Get the sender to show in the SMS</span>
      <span style="color: #0000ff;">sender=</span><span style="color: #ff0000;">&quot;$2&quot;</span>;
      <span style="color: #000066;">shift</span> <span style="color: #cc66cc;">2</span>;
      ;;
  -u<span style="color: #66cc66;">&#41;</span>
      <span style="color: #808080; font-style: italic;"># Get the username</span>
      <span style="color: #0000ff;">username=</span><span style="color: #ff0000;">&quot;$2&quot;</span>;
      <span style="color: #000066;">shift</span> <span style="color: #cc66cc;">2</span>;
      ;;
  -p<span style="color: #66cc66;">&#41;</span>
      <span style="color: #808080; font-style: italic;"># Get the password</span>
      <span style="color: #0000ff;">password=</span><span style="color: #ff0000;">&quot;$2&quot;</span>;
      <span style="color: #000066;">shift</span> <span style="color: #cc66cc;">2</span>;
      ;;
  -d<span style="color: #66cc66;">&#41;</span>
      <span style="color: #0000ff;">dry_run=</span><span style="color: #ff0000;">&quot;dry_run&quot;</span>;
      <span style="color: #000066;">shift</span> <span style="color: #cc66cc;">1</span>;
      ;;
  *<span style="color: #66cc66;">&#41;</span>
      <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;Unknown option: $1&quot;</span>
      <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">1</span>;
      ;;
    <span style="color: #b1b100;">esac</span>
<span style="color: #b1b100;">done</span>
 
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -z <span style="color: #ff0000;">&quot;$username&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
  logfail <span style="color: #ff0000;">&quot;No username specified or found in $config_file.&quot;</span>
<span style="color: #b1b100;">fi</span>
 
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -z <span style="color: #ff0000;">&quot;$password&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
  logfail <span style="color: #ff0000;">&quot;No password specified or found in $config_file.&quot;</span>
<span style="color: #b1b100;">fi</span>
 
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -z <span style="color: #ff0000;">&quot;$numbers&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
  logfail <span style="color: #ff0000;">&quot;No numbers specified or found in $config_file.&quot;</span>
<span style="color: #b1b100;">fi</span>
 
<span style="color: #0000ff;">message_length=</span>`<span style="color: #000066;">echo</span> -n <span style="color: #ff0000;">&quot;$message&quot;</span>|wc -c`
<span style="color: #0000ff;">sender_length=</span>`<span style="color: #000066;">echo</span> -n <span style="color: #ff0000;">&quot;$sender&quot;</span>|wc -c`
 
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">&quot;$message_length&quot;</span> -gt <span style="color: #ff0000;">&quot;160&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
  logfail <span style="color: #ff0000;">&quot;SMS message is longer than 160 chars ($message_length). That is not allowed.&quot;</span> &gt;&amp;<span style="color: #cc66cc;">2</span>
<span style="color: #b1b100;">fi</span>
 
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">&quot;$sender_length&quot;</span> -gt <span style="color: #ff0000;">&quot;11&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
  logfail <span style="color: #ff0000;">&quot;Sender is longer than 11 chars ($sender_length). That is not allowed.&quot;</span> &gt;&amp;<span style="color: #cc66cc;">2</span>
<span style="color: #b1b100;">fi</span>
 
<span style="color: #808080; font-style: italic;"># We haven't sent the message yet</span>
<span style="color: #0000ff;">message_sent_ok=</span><span style="color: #cc66cc;">0</span>;
 
<span style="color: #808080; font-style: italic;"># The API supports sending to a comma seperated list, but that doesn't seem</span>
<span style="color: #808080; font-style: italic;"># to work well. Therefore, I space seperate them and just call the API for</span>
<span style="color: #808080; font-style: italic;"># each number.</span>
<span style="color: #b1b100;">for</span> number <span style="color: #b1b100;">in</span> <span style="color: #0000ff;">$numbers</span>; <span style="color: #b1b100;">do</span> 
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> ! <span style="color: #ff0000;">&quot;$dry_run&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
    <span style="color: #0000ff;">RESPONSE=</span>`curl -s -d <span style="color: #0000ff;">username=</span><span style="color: #ff0000;">&quot;$username&quot;</span> -d <span style="color: #0000ff;">md5_password=</span><span style="color: #ff0000;">&quot;$password&quot;</span> -d <span style="color: #0000ff;">originator=</span><span style="color: #ff0000;">&quot;$sender&quot;</span> -d <span style="color: #0000ff;">recipients=</span><span style="color: #ff0000;">&quot;$number&quot;</span> -d <span style="color: #0000ff;">message=</span><span style="color: #ff0000;">&quot;$message&quot;</span> http://www.mollie.nl/xml/sms/`
  <span style="color: #b1b100;">else</span>
    <span style="color: #0000ff;">RESPONSE=</span><span style="color: #ff0000;">&quot;&lt;success&gt;true&lt;/success&gt;&quot;</span>
  <span style="color: #b1b100;">fi</span>
 
  <span style="color: #808080; font-style: italic;"># Curl was able to post okay...</span>
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> -eq <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
    <span style="color: #0000ff;">success_line_true=</span>`<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;$RESPONSE&quot;</span>|grep -i <span style="color: #ff0000;">&quot;&lt;success&gt;true&lt;/success&gt;&quot;</span>`
 
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -z <span style="color: #ff0000;">&quot;$success_line_true&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
      logfail <span style="color: #ff0000;">&quot;$message to $number. Response was: $RESPONSE&quot;</span>
    <span style="color: #b1b100;">fi</span>
  <span style="color: #b1b100;">else</span>
    logfail <span style="color: #ff0000;">&quot;curl return an error while trying to contact mollie to send an SMS.&quot;</span> &gt;&amp;<span style="color: #cc66cc;">2</span>
  <span style="color: #b1b100;">fi</span>
 
  <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -w <span style="color: #ff0000;">&quot;$log_file&quot;</span> <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;Success: [`date`]: $dry_run Sent '$message' to $number&quot;</span> &gt;&gt; <span style="color: #0000ff;">$log_file</span>
  <span style="color: #b1b100;">fi</span>
<span style="color: #b1b100;">done</span>
 
<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;Success sending sms(es).&quot;</span></pre>

<p>
It has the following config file in /etc/notify_sms.conf:
</p>

<pre class="php"><span style="color: #808080; font-style: italic;"># Default values. Can be overriden with command line arguments</span>
username=<span style="color: #ff0000;">&quot;halfgaar&quot;</span>
password=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #808080; font-style: italic;"># MD5sum of HTTP API password.</span>
sender=<span style="color: #ff0000;">&quot;Melk&quot;</span>
numbers=<span style="color: #ff0000;">&quot;+316xxxxxxxx&quot;</span> <span style="color: #808080; font-style: italic;"># You can set multiple numbers by space seperating them. </span></pre>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2009/12/12/bash-script-for-sending-sms-using-mollie/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash quoting</title>
		<link>http://blog.bigsmoke.us/2009/10/18/bash-quoting</link>
		<comments>http://blog.bigsmoke.us/2009/10/18/bash-quoting#comments</comments>
		<pubDate>Sun, 18 Oct 2009 15:12:25 +0000</pubDate>
		<dc:creator>halfgaar</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[quotes]]></category>
		<category><![CDATA[quoting]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=850</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>
I&#8217;m always confused by bash&#8217;s quoting. I hope to put all my quote wisdom in this post and invoke other&#8217;s quote wisdom in the comments. I&#8217;ll give some examples of what I mean.
</p>

<p>
Let&#8217;s say you have a file with a space: &#8220;bla bla.txt&#8221;. If I were to ls that file, I would do:
</p>

<pre class="php">ls <span style="color: #ff0000;">'bla bla.txt'</span></pre>

<p>
This works. However, when I want to do this from a variable (in a script) and do:
</p>

<pre class="php">command=<span style="color: #ff0000;">&quot;ls 'bla bla.txt'&quot;</span>
<span style="color: #0000ff;">$command</span></pre>

<p>
The result is: 
</p>

<pre>
ls: cannot access 'bla: No such file or directory
ls: cannot access bla.txt': No such file or directory
</pre>

<p>
You can solve this by using eval:
</p>

<pre class="php">command=<span style="color: #ff0000;">&quot;ls 'bla bla.txt'&quot;</span>
<a href="http://www.php.net/eval"><span style="color: #000066;">eval</span></a> <span style="color: #0000ff;">$command</span></pre>

<p>
This gives:
</p>

<pre>
bla bla.txt
</pre>

<p>
Some time ago, I suggested this as answer on somebodies question at userfriendly, to which somebody else said that <a href="http://ars.userfriendly.org/cartoons/read.cgi?id=20081027&#038;tid=2974519">using eval actually makes things worse</a>:
</p>

<blockquote>
<p>
That&#8217;s actually worse. . . as the quoting gets re-parsed (remember, &#8216;eval&#8217; means &#8220;take arguments as shell input&#8221;), which means that single quotes in the name break it, horribly, and names with spaces get even _worse_. 
</p>
</blockquote>

<p>
Another example: let&#8217;s say you have two files:
</p>

<pre>
-rw-r----- 1 halfgaar halfgaar 0 2009-10-18 16:51 bla's bla"s.txt
-rw-r----- 1 halfgaar halfgaar 0 2009-10-18 16:52 normal.txt
</pre>

<p>
I&#8217;m gonna run this command on it: find . -mindepth 1 -exec ls &#8216;{}&#8217; \;. When executed without eval, it says this:
</p>

<pre class="php">find: missing argument to `-<a href="http://www.php.net/exec"><span style="color: #000066;">exec</span></a><span style="color: #ff0000;">' </span></pre>

<p>
With eval, it says:
</p>

<pre>
./normal.txt
./bla's bla"s.txt
</pre>

<p>
Eval seems to be what I need, so what is wrong with using it? Also, shouldn&#8217;t that double quote be a problem? If someone can give a situation where that poses problems, I&#8217;m all ears.
</p>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2009/10/18/bash-quoting/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tracking WordPress in a Subversion vendor branch</title>
		<link>http://blog.bigsmoke.us/2009/09/20/svn-vendor-branches-wordpress</link>
		<comments>http://blog.bigsmoke.us/2009/09/20/svn-vendor-branches-wordpress#comments</comments>
		<pubDate>Sun, 20 Sep 2009 11:03:34 +0000</pubDate>
		<dc:creator>Rowan Rodrik</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Subversion]]></category>
		<category><![CDATA[svn]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://blog.bigsmoke.us/?p=824</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>Two months prior to writing a <a href="http://blog.bigsmoke.us/2009/07/20/svn-vendor-branches">script to upgrade MediaWiki installations using Subversion vendor branches</a>, I wrote something similar for WordPress. It&#8217;s a little bit more limited and should really incorporate some of the improvements made for the MediaWiki version, but it worked fine so far:</p>

<pre class="bash">cat upgrade-wordpress.sh 
<span style="color: #808080; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #0000ff;">svn_repo_url=</span>file:///var/svn/blog.omega-research.org/vendor/wordpress/
<span style="color: #0000ff;">last_version=</span>$<span style="color: #cc66cc;">1</span>
<span style="color: #0000ff;">new_version=</span>$<span style="color: #cc66cc;">2</span>
&nbsp;
<span style="color: #0000ff;">tmp_dir=</span>`mktemp -d`
<span style="color: #000066;">cd</span> <span style="color: #0000ff;">$tmp_dir</span>
&nbsp;
<span style="color: #b1b100;">for</span> version <span style="color: #b1b100;">in</span> $*; <span style="color: #b1b100;">do</span>
    <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;Downloading and extracting WordPress version $version...&quot;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -z `svn ls <span style="color: #0000ff;">$svn_repo_url</span>|grep <span style="color: #0000ff;">$version</span>` <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
        <span style="color: #0000ff;">branch=</span>`<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$version</span> |sed -e <span style="color: #ff0000;">'s/<span style="color: #000099; font-weight: bold;">\.</span>[0-9]<span style="color: #000099; font-weight: bold;">\+</span>$//'</span>`
        <span style="color: #0000ff;">archive_file=</span><span style="color: #ff0000;">&quot;wordpress-$version.tar.gz&quot;</span>
        <span style="color: #0000ff;">md5_file=</span><span style="color: #ff0000;">&quot;wordpress-$version.md5&quot;</span>
&nbsp;
        wget <span style="color: #ff0000;">&quot;http://wordpress.org/$md5_file&quot;</span>
        wget <span style="color: #ff0000;">&quot;http://wordpress.org/$archive_file&quot;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> `md5sum <span style="color: #0000ff;">$archive_file</span> |cut -f <span style="color: #cc66cc;">1</span> -d <span style="color: #ff0000;">' '</span>` != `cat <span style="color: #0000ff;">$md5_file</span>` <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
            <span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;MD5 sum did not match!&quot;</span> &gt;&amp;<span style="color: #cc66cc;">2</span>
            <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">1</span>
        <span style="color: #b1b100;">fi</span>
&nbsp;
        tar --extract --ungzip --transform <span style="color: #ff0000;">&quot;s/^wordpress/$version/&quot;</span> --file <span style="color: #0000ff;">$archive_file</span>
        svn_load_dirs.pl <span style="color: #0000ff;">$svn_repo_url</span> -t <span style="color: #0000ff;">$version</span> current <span style="color: #0000ff;">$version</span>
    <span style="color: #b1b100;">fi</span>
<span style="color: #b1b100;">done</span>
&nbsp;
<span style="color: #000066;">cd</span> -
svn merge <span style="color: #ff0000;">&quot;$svn_repo_url$last_version&quot;</span> <span style="color: #ff0000;">&quot;$svn_repo_url$new_version&quot;</span> .</pre>

<p>I&#8217;m actually planning to make both scripts a little bit more generic (in the sense that <tt>svn_repo_url</tt> becomes an external param) and to track future changes to them using GitHub&#8217;s <a href="http://gist.github.com/">Gist</a>. (How ironic is that, tracking an script for Subversion using Git?)</p>]]></content:encoded>
			<wfw:commentRss>http://blog.bigsmoke.us/2009/09/20/svn-vendor-branches-wordpress/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

