<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Ttmrichter's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 13 Oct 2008 22:42:33 GMT</pubDate>
    <description>DZone Snippets: Ttmrichter's Code Snippets</description>
    <item>
      <title>Ruby one-liner to find external IP address across NAT router.</title>
      <link>http://snippets.dzone.com/posts/show/3947</link>
      <description>Sometimes you just have to know what your external IP address is when there's a NAT between you and the rest of the world.  Say you're in a DHCP environment that's constantly changing your IP, for example.  This one-liner gives you your IP address as a string (in this case stored as "my_ip").&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;my_ip = (require 'open-uri' ; open("http://myip.dk") { |f| /([0-9]{1,3}\.){3}[0-9]{1,3}/.match(f.read)[0].to_a[0] })&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 02 May 2007 14:23:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3947</guid>
      <author>ttmrichter (Michael T. Richter)</author>
    </item>
    <item>
      <title>Making SQLITE/SQLITE3 executable scripts.</title>
      <link>http://snippets.dzone.com/posts/show/3080</link>
      <description>Use "here document" statements to build complex script files with embedded SQL statements via the sqlite/sqlite3 utility.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#! /usr/bin/env bash&lt;br /&gt;&lt;br /&gt;# execute some bash scripting commands here&lt;br /&gt;&lt;br /&gt;sqlite3 mydatabase &lt;&lt;SQL_ENTRY_TAG_1&lt;br /&gt;SELECT * &lt;br /&gt;  FROM mytable &lt;br /&gt;  WHERE somecondition='somevalue';&lt;br /&gt;SQL_ENTRY_TAG_1&lt;br /&gt;&lt;br /&gt;# execute other bash scripting commands here&lt;br /&gt;&lt;br /&gt;sqlite3 mydatabase &lt;&lt;SQL_ENTRY_TAG_2&lt;br /&gt;SELECT *&lt;br /&gt;  FROM myothertable&lt;br /&gt;  WHERE someothercondition='someothervalue';&lt;br /&gt;SQL_ENTRY_TAG_2&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note that being in a bash script means that you can expand $-variables inside the SQL code directly.  This is, however, not advised unless you can be sure that only trusted, competent people will run your code.  Otherwise you'll be facing SQL injection attacks.</description>
      <pubDate>Mon, 04 Dec 2006 18:54:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3080</guid>
      <author>ttmrichter (Michael T. Richter)</author>
    </item>
    <item>
      <title>Making MySQL executable scripts.</title>
      <link>http://snippets.dzone.com/posts/show/3078</link>
      <description>Use "here document" statements to build complex script files with embedded SQL statements via the mysql utility.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#! /usr/bin/env bash&lt;br /&gt;&lt;br /&gt;# execute some bash scripting commands here&lt;br /&gt;&lt;br /&gt;mysql mydatabase &lt;&lt;SQL_ENTRY_TAG_1&lt;br /&gt;SELECT * &lt;br /&gt;  FROM mytable &lt;br /&gt;  WHERE somecondition='somevalue';&lt;br /&gt;SQL_ENTRY_TAG_1&lt;br /&gt;&lt;br /&gt;# execute other bash scripting commands here&lt;br /&gt;&lt;br /&gt;mysql mydatabase &lt;&lt;SQL_ENTRY_TAG_2&lt;br /&gt;SELECT *&lt;br /&gt;  FROM myothertable&lt;br /&gt;  WHERE someothercondition='someothervalue';&lt;br /&gt;SQL_ENTRY_TAG_2&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note that being in a bash script means that you can expand $-variables inside the SQL code directly.  This is, however, not advised unless you can be sure that only trusted, competent people will run your code.  Otherwise you'll be facing SQL injection attacks.</description>
      <pubDate>Mon, 04 Dec 2006 18:34:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3078</guid>
      <author>ttmrichter (Michael T. Richter)</author>
    </item>
    <item>
      <title>Get HTML/XML output of your MySQL queries.</title>
      <link>http://snippets.dzone.com/posts/show/3077</link>
      <description>The mysql command line utility can be used to build databases, manipulate them, etc.  It can also be used as an ad-hoc query tool with HTML-snippet output.  Consider this code:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;mysql -e "SELECT * FROM mytable WHERE somecondition='somevalue'"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The resulting output will be a mess of +, - and | characters used to frame boxes around the values.  Now consider instead:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;mysql -H -e "SELECT * FROM mytable WHERE somecondition='somevalue'"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The output of this is &lt;TABLE/&gt;&lt;TR/&gt;&lt;TD/&gt; code that can be cut-and-pasted into your HTML editor of choice.  Complex queries not easily put into a single -e string can be done thus:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;mysql -H &lt; myqueries.sql&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note that no HTML is generated for any query that does not have a result set (like INSERT or UPDATE).&lt;br /&gt;&lt;br /&gt;Change the -H to a -X to get XML output (without a DTD/XSL description, unfortunately).</description>
      <pubDate>Mon, 04 Dec 2006 18:26:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3077</guid>
      <author>ttmrichter (Michael T. Richter)</author>
    </item>
    <item>
      <title>Just #@$% do it!</title>
      <link>http://snippets.dzone.com/posts/show/3076</link>
      <description>If you have an unreliable network feed and want to run something like rsync, scp, etc. without having to babysit it a simple function can be used to prefix a command.  The function looks like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Function to force a command to try until it works.&lt;br /&gt;# Name means "JUST #@$% DO IT!"&lt;br /&gt;JFDI () {&lt;br /&gt;  COMMAND=$*&lt;br /&gt;  while ! $COMMAND ; do echo "Retrying..." ; done&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Using it is simplicity itself:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# command that can fail and annoy&lt;br /&gt;rsync -avz /my/local/directory/ myuser@host:~/my/remote/directory/&lt;br /&gt;&lt;br /&gt;# command that won't give up ever&lt;br /&gt;JFDI rsync -avz /my/local/directory/ myuser@host:~/my/remote/directory/&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 04 Dec 2006 17:32:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3076</guid>
      <author>ttmrichter (Michael T. Richter)</author>
    </item>
  </channel>
</rss>
