George V. Reilly

Bash: Getting and Setting Default Values

Bash has some handy syntax for getting and setting default values. Un­for­tu­nate­ly, it’s a collection of punc­tu­a­tion characters, which makes it hard to Google when you can’t quite remember the syntax.

Getting a default value using ${var:-fallback}:

# set $LOGDIR to $1 if $1 has a value; otherwise set $LOGDIR to "/var/log"
LOGDIR="${1:-/var/log}"

# use $VERSION unless it's empty or unset; fall back to extracting someprog's version num
build_version=${VERSION:-$(someprog --version | sed 's/[^0-9.]*\([0-9.]*\).*/\1/')}

The colon-dash con­struc­tion is known as the dog’s bollocks in typography.

Setting a default value, using ${var:=fallback}:

$ echo $HOME
/Users/georgevreilly
$ echo ${HOME:=/tmp}
/Users/georgevreilly
$ unset HOME
$ echo ${HOME:=/tmp}
/tmp
$ echo $HOME
/tmp
$ cd; pwd
/tmp

Note: := uses the new value in two cases. First, when the shell continue.

Review: Blind Justice

Title: Blind Justice
Author: Bruce Alexander
Rating: ★ ★ ★ ★
Publisher: Berkeley
Copyright: 1994
Pages: 336
Keywords: historical mystery
Reading period: 10 January, 2016

The first in a series about Sir John Fielding, the blind magistrate who founded the Bow Street Runners, London’s first pro­fes­sion­al police force in 1749. Jeremy Proctor, a newly orphaned 13-year-old, is taken under Sir John’s wing and assists him in dis­cov­er­ing how the rakish Lord Goodhope was murdered in a locked room.

Although I figured out the murderer halfway through, I still enjoyed both the plot and the characters. Alexander vividly brings Georgian London to life.

Review: A Dedicated Man

Title: A Dedicated Man
Author: Peter Robinson
Rating: ★ ★ ★ ★
Publisher: Avon
Copyright: 1988
Pages: 352
Keywords: police procedural
Reading period: 8–10 January, 2016

A Dedicated Man is the second novel in the Inspector Banks series. A local historian has been murdered in the Yorkshire Dales. He was well-liked and there seems to be no obvious motives or suspects. Banks must dig into the dead man’s past if there are no leads in the present.

Like other books in the series, this is a competent well-written police procedural, partly seen through Banks’ eyes and partly through the eyes of some of the locals. Robinson is an expatriate York­shire­man with a fondness for his homeland and continue.

Review: Dollmaker

Title: Dollmaker
Author: J. Robert Janes
Rating: ★ ★ ★ ½
Publisher: Soho Crime
Copyright: 1995
Pages: 272
Keywords: police procedural, WW II
Reading period: 2–7 January, 2016

Occupied France, January 1943. Detectives Jean-Louis St-Cyr and Hermann Kohler are sent to the German submarine base at Lorient in Brittany to in­ves­ti­gate a murder. The Gross-Admiral wants a quick resolution to the case since the prime suspect is a U-Boat captain known as the Dollmaker, whose crew are de­mor­al­ized after many months of punishing cruises and who won’t go back to sea without him.

St-Cyr and Kohler are unlikely partners, a Chief Inspector from the Sûreté in Paris and a longtime criminal policeman now in the Gestapo. Both are astute continue.

Happy Birthday, Roy Batty

I’ve seen things you people wouldn’t believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhäuser Gate. All those moments will be lost in time, like tears…in…rain. Time to die.

—Roy Batty, the Tears in Rain monologue

According to Blade Runner, the replicant Roy Batty’s incept date is January 8th, 2016. The Sydney Morning Herald has an article.

In honor of which, we watched Blade Runner tonight.

LastPass and Diceware

My LastPass browser plugin just upgraded itself to v4.0. For several years, I’ve been using LastPass to manage all of my passwords. I have literally hundreds of passwords. I can’t even remember half the sites, much less the usernames. With LastPass, I can maintain a strong, distinct password for each site, which is robustly encrypted and backed up in the cloud, and I get good browser in­te­gra­tion and adequate Android in­te­gra­tion. We also use LastPass at work for our individual use and to share cre­den­tials.

There are still a handful of passwords that I have to remember and type, including the master password for my LastPass account, laptop passwords, and GPG passphras­es.

I’ve long used continue.

Review: Hide Me Among The Graves

Title: Hide Me Among The Graves
Author: Tim Powers
Rating: ★ ★ ★ ★
Publisher: William Morrow
Copyright: 2012
Pages: 544
Keywords: fantasy, secret history
Reading period: 2 December, 2015–6 January, 2016

The Rosetti siblings, Christina, Dante (Gabriel), Maria, and William, are haunted by the vampire who was once their uncle, John Polidori. The vampires pos­ses­sive­ly love certain humans and grant those humans great powers of creativity. Christina both loves her uncle and yearns to be free of him. The other humans who receive the attentions of the vampires likewise feel both a forbidden attraction and a horrified repulsion at their own potential damnation.

Tim Powers is known for his “secret histories”, wherein he takes historical events continue.

Decrementing Loops

The canonical for-loop in C and C++ is written thus, counting up i = 0, i = 1, …, i = N-1:

for (int i=0; i < N; i++) {
    // loop body
}

(In C, you have to declare int i before the for-loop.)

Let’s unpack that for-loop into an equivalent while-loop:

int i = 0;
while (i < N) {
    // loop body
    i = i + 1
}

In other words, we initialize i to zero. Then, before every execution of either loop, we check i < N. If i is still within bounds, we execute the loop body. Then we postin­cre­ment continue.

Obfuscating Passwords in URLs in Python

[Pre­vi­ous­ly published at the now defunct MetaBrite Dev Blog.]

RFC 1738 allows passwords in URLs, in the form <scheme>://<username>:<password>@<host>:<port>/<url-path>. Although passwords are deprecated by RFC 3986 and other newer RFCs, it’s oc­ca­sion­al­ly useful. Several important packages in the Python world allow such URLs, including SQLAlchemy ('post­gresql://scott:tiger@localhost:5432/my­data­base') and Celery ('amqp://guest:guest@localhost:5672//'). It’s also useful to be able to log such URLs without exposing the password.

Python 2 has urlparse.urlparse (known as urllib.parse.urlparse in Python 3 and six.moves.url­lib_­parse.urlparse in the Six com­pat­i­bil­i­ty library) to split a URL into six components, scheme, netloc, path, parameters, query, and fragment. The netloc cor­re­sponds to <user>:<password>@<host>:<port>.

Un­for­tu­nate­ly, neither Python 2 nor 3’s urlparse properly handle the userinfo (username + optional password in the netloc), as they continue.

Obfuscating Passwords in URLs in Python

New MetaBrite Dev Blog post, Ob­fus­cat­ing Passwords in URLs in Python.

Previous » « Next