Skip to content

Instantly share code, notes, and snippets.

View PeterJRiches's full-sized avatar

Pete Riches PeterJRiches

  • New Zealand
  • 00:50 (UTC +13:00)
View GitHub Profile
@PeterJRiches
PeterJRiches / does_command_change_files.sh
Created June 21, 2025 20:55
Detect whether a directory tree has changed (in bash, without diff)
# hash_the_dir ()
# Hash all the files in the given directory to produce one checksum.
# (Useful for checking for any file's contents or name being changed.)
# Parameter: directory
# Output: md5sum of the names and contents of all the files, recursively.
hash_the_dir() {
# The options below are to make the archive deterministic:
# i.e. same files, same hash; not affected by metadata (like timestamps),
# nor by any changes to the git repo, if present (like commits).
local error="Error calling hash_the_dir"
@PeterJRiches
PeterJRiches / sudomerge_pacnew.md
Created April 24, 2025 23:37
`sudomerge`: a `sudoedit` analog for merging `.pacnew` files in `/etc/`

sudoedit analog for merging .pacnew files

  • Purpose: semi-automate maintenance task
  • Scope: Arch-based Linux distros

Background

When distros using pamac (or one of it derivatives) for package management encounter conflicts between updated distribution config files and locally-edited config files, they preserve the local sysadmin's edits and save the distribution's recommended config file under <originalname>.pacnew in the same directory (/etc/**). It is the task of the sysadmin to merge the recommended changes from the distro packager into their own config files. The recommended changes account for updates in the OS and security patches, so it is a good idea to apply the changes regularly, and not to let the *.pacnew files accumulate unheeded.

@PeterJRiches
PeterJRiches / Prevent_unintentional_bash_self-modification.md
Last active April 8, 2025 05:28
Prevent unintentional self-modification of executing Bash script

Prevent unintentional self-modification of executing Bash script

Rationale

I have a script that downloads and synchronises a project from a web service (where I co-edit this project) to my local machine, where I have the project's Git repo.1

Bash does not read a whole script file in at once, rather reading it in chunks as needed for interpretation. Therefore, it is possible for the file to be modified2 while the script is running. This is seldom useful, unless you want to be really esoteric!

Footnotes

  1. The web service does have paid options for Git or GitHub integration, but this project is not generating any income, so although I'd love to follow best practice, I have to fudge it for now.

  2. --modified in place, not replaced with a new file of the same name: Were the file to be replaced, the script would continue reading the original file from the original inode, and the new file would be assigned a new inode by the operating system, despite it having the same name.

@PeterJRiches
PeterJRiches / sync-humanforce.md
Last active November 29, 2023 06:00
Sync Humanforce with Google Calendar

Sync Humanforce Rostering with Google Calendar

I work irregular hours and seldom know what my roster is, further than the next workday, and even then I am unsure of details. I need access to a computer to check, and I have to boot, authenticate and wait. It would be simple if it was in my calendar and I only had to reach for my phone. Google Calendar plays well with other apps on Android and other platforms, improving accessibility, for example for people with ADHD.

The Humanforce web interface provides a button to export a VCalendar (ICS) file. The endpoint is

@PeterJRiches
PeterJRiches / lorem-ipsum
Created December 22, 2022 05:51
lorem-ipsum from shell
# REST API can be json or xml
wget --output-document lorem 'http://www.lipsum.com/feed/json?what=paragraphs&amount=2&start=yes'
@PeterJRiches
PeterJRiches / scan-rolls.md
Last active April 8, 2025 05:22
Want to scan arbitrary-length paper, rather than pages, in an optical scanner

Want to scan arbitrary-length paper, rather than pages

I have code listings from old projects on rolls of paper, or fanfold stacks of paper. Either way, the paper is continuous, not split into pages. (My projects predated page-printers! The old printers took continuous feedstock.)

I want to scan (and ideally use OCR to extract text from) my old listings.

To date, I haven't found any open source projects to make a scanner with a sheet-feeder continue to scan after the end of what it deems a page.