Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Michael T. Richter

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Ruby one-liner to find external IP address across NAT router.

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").

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] })

Making SQLITE/SQLITE3 executable scripts.

Use "here document" statements to build complex script files with embedded SQL statements via the sqlite/sqlite3 utility.

#! /usr/bin/env bash

# execute some bash scripting commands here

sqlite3 mydatabase <<SQL_ENTRY_TAG_1
SELECT * 
  FROM mytable 
  WHERE somecondition='somevalue';
SQL_ENTRY_TAG_1

# execute other bash scripting commands here

sqlite3 mydatabase <<SQL_ENTRY_TAG_2
SELECT *
  FROM myothertable
  WHERE someothercondition='someothervalue';
SQL_ENTRY_TAG_2


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.

Making MySQL executable scripts.

Use "here document" statements to build complex script files with embedded SQL statements via the mysql utility.

#! /usr/bin/env bash

# execute some bash scripting commands here

mysql mydatabase <<SQL_ENTRY_TAG_1
SELECT * 
  FROM mytable 
  WHERE somecondition='somevalue';
SQL_ENTRY_TAG_1

# execute other bash scripting commands here

mysql mydatabase <<SQL_ENTRY_TAG_2
SELECT *
  FROM myothertable
  WHERE someothercondition='someothervalue';
SQL_ENTRY_TAG_2


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.

Get HTML/XML output of your MySQL queries.

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:

mysql -e "SELECT * FROM mytable WHERE somecondition='somevalue'"


The resulting output will be a mess of +, - and | characters used to frame boxes around the values. Now consider instead:

mysql -H -e "SELECT * FROM mytable WHERE somecondition='somevalue'"


The output of this is <TABLE/><TR/><TD/> 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:

mysql -H < myqueries.sql


Note that no HTML is generated for any query that does not have a result set (like INSERT or UPDATE).

Change the -H to a -X to get XML output (without a DTD/XSL description, unfortunately).

Just #@$% do it!

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:

# Function to force a command to try until it works.
# Name means "JUST #@$% DO IT!"
JFDI () {
  COMMAND=$*
  while ! $COMMAND ; do echo "Retrying..." ; done
}


Using it is simplicity itself:

# command that can fail and annoy
rsync -avz /my/local/directory/ myuser@host:~/my/remote/directory/

# command that won't give up ever
JFDI rsync -avz /my/local/directory/ myuser@host:~/my/remote/directory/
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS