New version of Squa.sh

posted 2009-04-20 22:57:28, link to this article

I've made some changes to squa.sh, my Bash script Pong clone.

The first version made extensivce use of tput(1) to update the terminal cursor position.
I compared the resulting control sequences sent to various terminal versions, and all seemed to be in the format:

    ^[${row};${column}H

Therefore this new version uses pre cooked control sequences instead of forking tput like mad. dd(1) is still spawned each iteration of the main loop to capture user input, read(1) smallest timeout is 1 second, so this is no option, or the game would be no much fun. ;)

Also terminal size detection is fixed for NetBSD now, which means that the new version of squa.sh runs on all hardware in my house right now!

You can grab a copy of the new version at http://isquared.nl/src/squa.sh-ng/squa.sh.

Sorting IP addresses

posted 2009-04-13 09:28:03, link to this article

Ever noticed how the Unix sort command can't make anything of IP addresses when you use just a numeric sort, like this:

hessch@galileo:~$ sort -n ip.txt
1.2.3.4
5.6.7.8
10.200.219.5
10.20.30.40
10.3.5.6
89.2.177.21
193.18.4.1

As you notice, for instance 10.20.30.40 is listed below 10.200.219.5, which is wrong, of course. The trick is to define every octect in the dotted quad notation as a key for sort like this:

hessch@galileo:~$ sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4 ip.txt
1.2.3.4
5.6.7.8
10.3.5.6
10.20.30.40
10.200.219.5
89.2.177.21
193.18.4.1

There you have it, using sort -t. -n -k1,1 -k2,2 -k3,3 -k4,4 all addresses are sorted properly.