Pragmatism in today's world

Jumping to the end of bash's history

I use bash's history all the time, via ctrl+r and also with the up and down keys; it's wonderful. Sometimes, I want to get back to the end of my history and I recently discovered that there's a shortcut for this: meta+>. It doesn't matter where you are in your history, pressing meta+> jumps you to the end and you have a blank prompt again. I use iTerm2 on my Mac and have my right… continue reading.

Stop in-place editing of bash history items

Recently, since getting a new computer, I've noticed that I've been losing bash history items and it took a while to work out what was going on, though I'm still not completely sure as it never seemed to be so much of a problem. I regularly use the up and down keys with context specific history. For example, I will type ma and then press up to search back through all the make commands I've… continue reading.

Notarising a macOS standalone binary

I've been writing a simple Swift command line tool called QuickSS. It's a single file swift file, that I compile to a standalone binaryusing: swiftc quickss.swift -o quickss To distribute it on modern Macs, I need to sign it and then get Apple to notarise it. Signing the binary To sign the binary, you need a "Developer ID Application" certificate from your paid developer account. If you don't have one there already create a new… continue reading.

Using the 1Password CLI in a script

I'm currently writing a script that notarises a macOS CLI app which needs to access a password. Rather than put it in an environment variable, I thought I'd use the 1Password CLI. This is the first time I've used it, so these are my notes. The 1Password CLI tool is call op. I installed it via Homebrew with: brew install 1password-cli Sign in You need to sign in. op signin As I have multiple accounts… continue reading.

Missing trailing new line when using cat in a bash script

Recently, I was writing a bash script that read a file into a string and then passed that string elsewhere that then calculated the hash of it. To my surprise, I was getting a different hash to doing the equivalent in PHP with file_get_contents(). Digging into it, I discovered that when you assign the output of cat to a variable, the trailing new line in the file is stripped off, which explained the difference. You… continue reading.

Use xxd to convert to hex

If you want to see the hex values of a file, the easiest way to do this is to use xxd. Given foo.txt that contains "This is some text!": $ xxd foo.txt 00000000: 5468 6973 2069 7320 736f 6d65 2074 6578 This is some tex 00000010: 7421 0a t!. There are three columns in the output: number of first character in hex Hex representation of up to 16 bytes, separated every 2 bytes ASCII representation,… continue reading.

Indenting output in a shell script with pr

When printing output in a shell script, it's quite useful to be able to indent information to group it. The easiest way to do this is with pr, like this: body=$(curl -s "http://localhost:8888/") echo "Response:" echo "$body" | pr -to 4 This generates: Response: { "links": { "games": "/games" } } Given that pr appeared in version 1 of AT&T Unix, I'm late to the party with this one, but it's a useful thing to… continue reading.

Getting status code and body from curl in a bash script

When writing a shell script recently, I realised that it would be really handy to get the the status code from a curl command in addition to the body. Usually, I call curl like this: body=$(curl -s "http://localhost:8888/") echo "Curl exit code: $?" echo "Body: $body" This works well, but no status code is available as curl's exit code is related to its own error system, not the HTTP call. Searching Stack Overflow, I came… continue reading.

Renaming PDF files based on their content

My accountant recently moved my business accounts system over to FreeAgent. One thing I like to do is keep a copy of every invoice PDF that I issue in a folder on my computer as a back up, just in case the online systems let me down. As I was learning new systems, I took advantage of this time to write a script to rename the invoice PDFs from Nineteen-Feet-Limited_Invoice_123.pdf to my preferred format of… continue reading.

Small scripts for repetitive tasks

One thing that I like to do is write a script for seemingly trivial things that I do more than once. I do this as it turns out that I end up needing them again. One example is a pair of script I use when testing rst2pdf. rst2pdf's tests work by creating a PDF and then comparing this to a reference PDF. If they are different, then the test has failed. When a test fails,… continue reading.