I recently decided to set myself up with a Unix box in the cloud. I want it so I can use it for things I can’t do on my iPad, most notably using
pandoc for converting MarkDown documents to various other formats.
There are any number of places that offer an incredibly cheap box. I found a special offer at one and bought myself a year for less than $20. I then fired up an Ubuntu 16 server.
Here’s what I did to get it all working in a (fairly) secure manner.
My daughter, Jessica was born the Thursday before Father’s Day in 1992, the 3rd of September. That means this year her birthday occurs on Father’s Day.
That begs the question, what other years will this occur? Here’s some Python code that answers the question.
# Find all the years when my daughter's
# birthday and Father's day coincide
# from the year of her birth till 2100
for i in range(1992, 2100):
dt = "3 09 " + str(i)
tt = time.strptime(dt, "%d %m %Y")
if tt.tm_wday == 6:
I love you, Jessica. My favourite Father’s Day gift ever, always.
I have spent my spare time in the past two weeks writing a web app. I couldn’t find a good web site that offered an easy personal journal. I wanted something that was a bit like a blog but with less fuss and totally private.
So I decided to try writing one. The language choice was obvious, Python. The next question was which framework to use. I did a web search and discovered that the choice quickly came down to two.
Django is the older giant in the room compared to Flask. The two also have a different philosophies. Django has it all built-in while Flask seems to be a thin wrapper over Werkzeug and Jinja2 mostly providing request and session handling while leaving almost everything else to extensions. Continue reading
This is more in the way of a note to myself. I was just starting to write a bash completion script for Pandoc when I came upon this in the Pandoc documentation:
Generate a bash completion script. To enable bash completion with pandoc, add this to your
eval "$(pandoc --bash-completion)"
So no need for me to write one. Neat trick, generate your own bash completion script. John McFarlane really is a god. Oh, and the completion is top quality, it knows when you’ve typed an option that takes an input or output format and completes on those and other little tricks. I may end up using some of his tricks for my completions.
Last night I went to Lights For The Wild at Taronga Zoo. As usual, I took a lot of photos with my DSLR camera, over 200, though a lot of that number are quite similar as I often take two or three to increase the chances of getting the right shot, sometimes I vary the speed so that one is better exposed.
The camera saves both a RAW file and a JPEG so I end up with over 400 images. Looking through them in QuickLook in the Finder can be painful as the RAW images take quite a while to load, then you get the problem that when you have decided which of the three shots you want to keep you also have to delete the matching JPEG or RAW file.
The easiest solution to both of these is to only go through the JPEG files and then delete the matching NEF file (which is what the Mac calls the RAW file).
So I open the folder and sort by ‘Kind’ which puts the JPEGs at the top. I then open the first in QuickLook by hitting space and using the up and down arrow keys to move through the list command–delete deletes a file and displays the next. Easy.
Now I have 80 JPEG files from the original 240. How to get rid of the NEF files that match the JPEG files I have deleted? A little
bash programming to the rescue.
for i in *.NEF ; do
if [ ! -e `basename $i NEF`JPG ]; then
The secret to this is the
basename utility. It’s a neat little tool. Pass it a full file path such as
/Users/tonyw/Documents/UselessRamblings.txt and it will return just
the file name without the path,
UselessRamblings.txt. It has a matching tool,
dirname which returns just the path portion. As you can see from my code basename has another trick, it will happily strip the suffix from the filename if you tell it what to strip.
I’m working on more bash completions. This time for some of the command line tools Apple provides for sysadmins.
I decided there had to be a way to get a list of the options from the
man page for a tool. After all they are all in there.
So I built a command line piece by piece. As an example let’s get a list of the options (with some caveats) for the tool
pkgbuild. We start with
man pkgbuild | col -b , the
col -b step takes out the special characters
man uses to show bold on screen. Now find all lines containing
grep, I liked
grep -e '--'. If you have a look at the output of that we are getting close.
Next I decided to use
sed to do a find and replace for the option itself. After some playing around I ended up with
sed -e 's#.*\(--[a-zA-Z-]*\).*$#\1#' An important note for young players, it did take some time and a few tries to get that substitution just right. Don’t be afraid and remember Google (and Stack Exchange) are your friends.
First, I should point out an old Unix hand’s trick. Most of the time you see
sed substitution commands using
/ as the separator but sed can use anything but
\ or newline – it uses the first character it sees after the ‘s’. I usually use
# as it makes the
\ used for special characters easier to spot and the string easier to read.
I’m on a roll. I’ve written the bash completions for Munki.
(tl;dr The completions are on Github )
It’s getting easier to write them. There was one little trick I used that I didn’t
mention in my last post that I thought I’d share. How to use find and replace with
regular expressions to generate some of your code.
For this I use
Find... in BBEdit. I started with a list of the commands, one on each
line. Continue reading