From 09960992096865d9556323ac5cddf4adbac95d55 Mon Sep 17 00:00:00 2001 From: Dominic Ricottone Date: Wed, 12 Oct 2022 12:25:21 -0500 Subject: [PATCH] New shebangs script; fixes to tar utilities and hugo-post script The shebangs script helps me manage dependency listings. The tar utilities were failing with to-directory options or passphrase options containing space characters. The fix requires some unappealing bash-isms, but at least it works. hugo-post now will work from the root directory of a Hugo project. --- archives/tarcat | 4 ++-- archives/tarls | 4 ++-- archives/unarchive.bash | 4 +++- archives/untar | 6 +++--- core/shebangs | 43 +++++++++++++++++++++++++++++++++++++++++ documents/hugo-post | 12 +++++++++--- 6 files changed, 62 insertions(+), 11 deletions(-) create mode 100755 core/shebangs diff --git a/archives/tarcat b/archives/tarcat index 06cc2a0..468bd50 100755 --- a/archives/tarcat +++ b/archives/tarcat @@ -35,7 +35,7 @@ while [[ $# -gt 0 ]]; do ;; -p|--passphrase) - passphrase="--passphrase $2" + passphrase="$2" shift; shift ;; @@ -60,7 +60,7 @@ done code=0 for archive_fn in "${positional[@]}"; do archive_action="$(archive_extension "$archive_fn" | tee >(>&2 /usr/bin/head -n -1) | /usr/bin/tail -n 1)" - if ! unarchive --archive=$archive_action --stdout "$archive_fn" $passphrase; then + if ! unarchive --archive=$archive_action --stdout "$archive_fn" ${passphrase:+--passphrase "$passphrase"}; then code=1 fi done diff --git a/archives/tarls b/archives/tarls index 7e57889..8034b7a 100755 --- a/archives/tarls +++ b/archives/tarls @@ -35,7 +35,7 @@ while [[ $# -gt 0 ]]; do ;; -p|--passphrase) - passphrase="--passphrase $2" + passphrase="$2" shift; shift ;; @@ -60,7 +60,7 @@ done code=0 for archive_fn in "${positional[@]}"; do archive_action="$(archive_extension "$archive_fn" | tee >(>&2 /usr/bin/head -n -1) | /usr/bin/tail -n 1)" - if ! unarchive --archive=$archive_action --list "$archive_fn" $passphrase; then + if ! unarchive --archive=$archive_action --list "$archive_fn" ${passphrase:+--passphrase "$passphrase"}; then code=1 fi done diff --git a/archives/unarchive.bash b/archives/unarchive.bash index 51c9295..6c78244 100644 --- a/archives/unarchive.bash +++ b/archives/unarchive.bash @@ -358,7 +358,9 @@ unarchive() { ;; *) - archive_fn="$(realpath "$1")" + if [[ ! -z "$1" ]]; then + archive_fn="$(realpath "$1")" + fi shift ;; esac diff --git a/archives/untar b/archives/untar index 32c6c5d..462ca13 100755 --- a/archives/untar +++ b/archives/untar @@ -27,7 +27,7 @@ while [[ $# -gt 0 ]]; do -d|--to-directory) debug_msg "Setting to_directory option to '${2}' (was ${to_directory})" - to_directory="--directory $2" + to_directory="$2" shift; shift ;; @@ -44,7 +44,7 @@ while [[ $# -gt 0 ]]; do -p|--passphrase) debug_msg "Setting passphrase option to '${2}' (was ${passphrase})" - passphrase="--passphrase $2" + passphrase="$2" shift; shift ;; @@ -69,7 +69,7 @@ done code=0 for archive_fn in "${positional[@]}"; do archive_action="$(archive_extension "$archive_fn" | tee >(>&2 /usr/bin/head -n -1) | /usr/bin/tail -n 1)" - if ! unarchive --archive=$archive_action "$archive_fn" $passphrase $to_directory; then + if ! unarchive --archive=$archive_action "$archive_fn" ${passphrase:+--passphrase "$passphrase"} ${to_directory:+--directory "$to_directory"}; then code=1 fi done diff --git a/core/shebangs b/core/shebangs new file mode 100755 index 0000000..fa66f45 --- /dev/null +++ b/core/shebangs @@ -0,0 +1,43 @@ +#!/bin/sh + +name="shebangs" +version="1.0" +usage_message="Usage: shebangs [PATH]" +help_message=$(/usr/bin/cat <<-EOF + Print the shebang line of each executable script + Usage: shebangs [PATH] + Options: + -h, --help print this message and exit + -v, --version print version number and exit +EOF +) + +. /usr/local/lib/myminiparse.sh + +# loop through arguments +dir= +for arg; do + case "$arg" in + -q|--quiet) + #ignore these + ;; + + *) + if [ -z "$dir" ]; then + dir="$arg" + fi + ;; + esac +done + +if [ -z "$dir" ]; then + dir="." +fi + +/usr/bin/find "$dir" -maxdepth 1 -type f -printf "%f\0" \ + | /usr/bin/sort -z \ + | while IFS= read -r -d '' fn; do + /usr/bin/printf '%s:' "$fn" + /usr/bin/head --lines=1 "$fn" +done | grep --regexp=':#!' | column --separator ':' --table + diff --git a/documents/hugo-post b/documents/hugo-post index e6caf7a..42b9412 100755 --- a/documents/hugo-post +++ b/documents/hugo-post @@ -39,12 +39,18 @@ fi # check if title can be made into a new filename filename="$(/usr/bin/printf "$title" | /usr/bin/tr --squeeze-repeat ' ' '_' | /usr/bin/tr --complement --delete '[:alnum:]_-').md" -if [ -f "$filename" ]; then +path="content/posts/${filename}" +if [ ! -d "content" ] || [ ! -d "content/posts" ]; then + if [ "$quiet" -eq 0 ]; then + (>&2 /usr/bin/printf "%s: not in a Hugo directory\n" "$name") + fi + exit 1 +elif [ -f "$path" ]; then if [ "$quiet" -eq 0 ]; then (>&2 /usr/bin/printf "%s: '%s' already exists\n" "$name" "$filename") fi exit 1 -elif [ -d "$filename" ]; then +elif [ -d "$path" ]; then if [ "$quiet" -eq 0 ]; then (>&2 /usr/bin/printf "%s: '%s' is a directory\n" "$name" "$filename") fi @@ -52,5 +58,5 @@ elif [ -d "$filename" ]; then fi # create new file -/usr/bin/printf -- '---\ntitle: %s\ndate: %s\n---\n\n' "$title" "$(hugo-date)" >"$filename" +/usr/bin/printf -- '---\ntitle: %s\ndate: %s\ndraft: true\n---\n\n' "$title" "$(hugo-date)" > "$path" -- 2.45.2