Bash: Use previous expression parameters

I was running a bash command similar to this:

perl /var/www/scott/perl/test/ --size 10 --file /tmp/output.txt

and afterwards I wanted to make a small change to that script. Rather than typing out the full path you can use some Bash history expansion and do this instead:

vim !:1

If you use !: followed by a number, Bash will replace that with the Xth (starting at zero) parameter from the previous command.

Leave A Reply

Reddit: Pinky and the Brain theory

Mind Blown!

The Idea is, that Pinky is in fact, a genius who lives with the insane Brain to keep him in check and to make sure he doesn't do anything too dangerous. There is a good amount of evidence to support this in the show.

We see a few examples, starting with the introduction song. There is a line that goes, "One is a genius, the other is insane, pinky and the brain." We already see a few hints from this. During the course of the show, there is never a question that pinky is not insane. An imbecile? Most certainly, but we never have a reason to believe he is insane. On the other hand, Brain is clearly insane - he is constantly trying to devise ways to take over the world. If Brain is infact insane, this leaves pinky to be, well, the genius

The biggest piece of evidence however, comes from an episode where Brain creates a machine to make Pinky smart. Pinky steps into it and begins to massively outsmart Brain - winning game shows, correcting him, and at the end of the episode - He shows how Brain's machine is inherently flawed and doesn't work at all. Almost immediately afterwords he returns to his bumbling self.

The begs the obvious question - if the machine never worked, how was Pinky so smart, and able to actually point out this design error? Simple, he is infact the genius.

Finally, there's the fact that Brain's inventions almost never work - they blow up in his face, and when they do work, they are sabotaged by none other than Pinky.

There's several more examples within the show, but when you look at the whole it becomes very convincing, that Brain is in fact being watched by Pinky, who is playing off the whole affair.

Borrowed from Ginnex on

Leave A Reply

SQLite: Case insensitive search

If you want to search a field in SQLite you might use a query like this:

SELECT MyID FROM Table WHERE Name = 'Doolis';

This performs a case-sensitive search on the Name field. If you want to search the name field in a case-insensitive manner do this:

Leave A Reply

Linux: Fedora 22 major package versions

Fedora 22 has been released and I gathered the versions of some core packages:

Package Version
Perl 5.20.2
PHP 5.6.9
Vim 7.4.640
Apache 2.4.12
Kernel 4.0.4
Leave A Reply

Linux: Reverse path forwarding headaches

The Linux kernel has a security feature called Reverse Path Forwarding which is designed to ensure that incoming packets are valid for your network. It validates that a packet arriving via a given interface has a valid IP address for that interface. In some situations a packet can arrive on one interface, and leave on a separate interface. If you have a packet like this Reverse Path Filtering kicks in and drops that packet.

This manifests in that you can see the packet arrive (via tcpdump) but nothing after that (i.e. the packet doesn't leave). The Linux IP stack drops the packet before any routing or service can act upon the packet. To log affected packets to syslog you can run:

echo 1 > /proc/sys/net/ipv4/conf/<interfacename>/log_martians

To disable this check completely you can run the following command:

for i in /proc/sys/net/ipv4/conf/*/rp_filter ; do
    echo 0 > $i 

Valid options for rp_filter are:

0 - No source validation.
1 - Strict mode as defined in RFC3704 Strict Reverse Path - Each incoming packet is tested against the FIB and if the interface is not the best reverse path the packet check will fail. By default failed packets are discarded.
2 - Loose mode as defined in RFC3704 Loose Reverse Path - Each incoming packet’s source address is also tested against the FIB and if the source address is not reachable via any interface the packet check will fail.

You can view the current settings for each interface on your box with this command:

sysctl -a | grep -E "net.ipv4.*\.rp_filter"

To make any changes permanent across a reboot set them in /etc/sysctl.conf

net.ipv4.conf.eth0.rp_filter = 0

Keywords: route, loop, egress, ingress, alien

Leave A Reply

Perl: Working with columnar data

I have a text file of data that is in whitespace separated columns that I need to work with. Perl has a command line option -a to enable auto-splitting the input into an array called @F. Using a Perl one-liner you can automatically split at whitespace separation like this:

cat /tmp/file_list.txt | perl -lane 'print "mv $F[3] $F[1]"'

This will output mv commands to rename the file in the 4th column to the 2nd column.

More information available in perlrun.

Leave A Reply

Linux: Debian 8 major package versions

Debian 8 has been released and I gathered the versions of some core packages:

Package Version
Perl 5.20.2
PHP 5.6.7
Vim 7.4.488
Apache 2.4.10
Kernel 3.16.0
Leave A Reply

Vim: Creating a portable copy of your configuration

If you use Vim on any regular basis you've probably created your own custom .vimrc file, and maybe installed a plugin or two. This config is machine specific and is not the easiest thing to move from one machine to another. I found this cool project called myvim that packages up your entire Vim installation into a single portable file. This file is a self-extracting archive of your Vim config that you can transfer to a new machine.

myvim -j /tmp/vim.bakers

This will create a file /tmp/vim.bakers which you can transfer and then run on a new machine.

Leave A Reply

Fedora: Force memcached to only listen on

I've been toying with memcached lately and wanted to ensure that it was only listening on On Fedora this is controlled by the file /etc/sysconfig/memcached. Make sure that the OPTIONS line contains -l

$ cat /etc/sysconfig/memcached 

Leave A Reply

Linux: Count CPU instructions

Zend published this infographic and it got me thinking about CPU instructions. According to the graphic, the Wordpress homepage required 9.4 billion machine instructions to render, but they've optimized PHP7 and it's now down to 2.6 billion. To count CPU Instructions on a Linux box you can use the perf command:

perf stat -e instructions <my_command>

In comparison, some very simple Linux commands take a significant amount of instructions:

# Approximately 640,000 instructions
perf stat -e instructions echo '' 

# Approximately 2 million instructions
perf stat -e instructions cd ~

# Approximately 700,000 instructions
perf stat -e instructions clear
Leave A Reply

Linux: Parallel file compression

Compressing files on Linux has gone through many iterations over the years. The three main methods to compress a file on Linux are: gzip, bzip2, and xz. All of these compression methods are single threaded, and can be "slow" on todays multi-core CPUs because they do not fully utilize all the cores. Fortunately the community has addressed this and created parallel implementations of each:

Single Threaded Parallel Version
gzip pigz
bzip2 pbzip2
xz pxz

All of these are available via Yum in Fedora and CentOS.

Leave A Reply

PHP: flat_var_export()

I needed to log a nested array to a file. To do this I needed to convert the array to a single line. After some searching I found improved_var_export() that converts a PHP array/object to a single line textual representation. I cleaned up some of the formatting and this is the result.

function flat_var_export($variable, $return = false) {
    if ($variable instanceof stdClass) {
        $result = '(object) ' . flat_var_export(get_object_vars($variable), true);
    } elseif (is_array($variable)) {
        $array = array();
        foreach ($variable as $key => $value) {
            $array[] = var_export($key, true) . ' => ' . flat_var_export($value, true);
        $result = 'array(' . implode(', ', $array) . ')';
    } else {
        $result = var_export($variable, true);

    if ($return) {
        return $result;
    } else {
        print $result;
Leave A Reply

RSYSLOG: Send email notifications

I want to monitor my log files for a specific line, and if found send an e-mail alert. Rsyslog has this built in, and it works great. I put the following config in /etc/rsyslog.g/dhcp-watch.conf and now I get an email if a specific DHCP subnet is used.

# If we give out DHCP for a specific network send an alert email
$ModLoad ommail


$template mailSubject,"DHCP Poll activity on %hostname%"
$template    mailBody,"DHCP Activity on the VLAN 873 pool\r\n\r\n%msg%"

$ActionMailSubject mailSubject

# Only send an email every 15 minutes
$ActionExecOnlyOnceEveryInterval 900

# This if/then must all be on one line
if $msg contains 'DHCPOFFER on 10.1.9.' then :ommail:;mailBody
Leave A Reply

PHP: Quote Word

I needed a function similar to Perl's qw. If you pass a string to this function it will return an array of the words, stripping any separating whitespace. If you pass true as the second parameter you will instead get a hash returning each word in a key/value pair.

function qw($str,$return_hash = false) {
    $str = trim($str);

    // Word characters are any printable char
    $words = str_word_count($str,1,"!\"#$%&'()*+,./0123456789-:;<=>?@[\]^_`{|}~");

    if ($return_hash) {
        $ret = array();
        $num = sizeof($words);

        // Odd number of elements, can't build a hash
        if ($num % 2 == 1) {
            return array();
        } else {
            // Loop over each word and build a key/value hash
            for ($i = 0; $i < $num; $i += 2) {
                $key   = $words[$i];
                $value = $words[$i + 1];

                $ret[$key] = $value;

            return $ret;
    } else {
        return $words;

This is useful in the following scenarios:

$str  = "Leonardo    Donatello    Michelangelo    Raphael";
$tmnt = qw($str);

$str = "
    Leonardo       Blue
    Donatello      Purple
    Michelangelo   Orange
    Raphael        Red
$turtles = qw($str,true);
Leave A Reply

Books of 2015

Also see the list of 2014. The date indicated denotes the date I started reading the book.

2015-01-08 - Ogre, Ogre - 307 pages
2015-01-14 - Divergent - 489 pages
2015-01-23 - Protector - 218 pages
2015-02-09 - Gone Girl - 415 pages
2015-02-23 - Night Mare - 307 pages
2015-03-06 - Ender in Exile - 464 pages
2015-03-26 - Harry Potter and the Goblet of Fire - 734 pages
2015-04-15 - Interview with the Vampire - 346 pages
2015-05-06 - Fahrenheit 451 - 179 pages
2015-05-15 - Dragon on a Pedastal - 306 pages
2015-05-29 - The Forever War - 202 pages
2015-06-17 - Redshirts - 317 pages
2015-06-27 - Goosebumps: The Curse of Camp Cold Lake - 114 pages
2015-07-02 - Harry Potter and the Order of the Phoenix - 870 Pages
2015-08-10 - Among Others - 302 Pages
2015-08-21 - Mr Mercedes - 436 Pages

Leave A Reply