From f273bcdcf7acb7c69f1ae279d9f20a8b58726f06 Mon Sep 17 00:00:00 2001 From: uvok cheetah Date: Wed, 18 Dec 2024 17:39:22 +0100 Subject: Add shell script for editing --- editpost.sh | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 editpost.sh diff --git a/editpost.sh b/editpost.sh new file mode 100644 index 0000000..2c0213b --- /dev/null +++ b/editpost.sh @@ -0,0 +1,49 @@ +#!/bin/bash + + +EDITOR_COMMAND="${EDITOR:-nano}" +if [[ -z "$1" ]]; then + echo "Usage: $0 " + exit 1 +fi + +update_last_modified_at() { + local file="$1" + local current_date="$(date +"%Y-%m-%d %H:%M %z")" + if grep -q '^last_modified_at:' "$file"; then + sed -i "s|^last_modified_at:.*|last_modified_at: $current_date|" "$file" + else + sed -i "/^date:/a last_modified_at: $current_date" "$file" + fi +} + +pattern="_posts/$1" +files=( $pattern ) + +if [[ (${#files[@]} -eq 1 && ! -e "${files[0]}") || ${#files[@]} -eq 0 ]]; then + echo "No files found matching pattern: $1" + exit 1 +fi + +if [[ ${#files[@]} -eq 1 ]]; then + update_last_modified_at "${files[0]}" + "$EDITOR_COMMAND" "${files[0]}" + exit 0 +fi + +echo "Multiple files found:" +i=0 +for f in "${files[@]}"; do + echo "$i) $f" + ((i++)) +done + +read -p "Select a file number: " choice + +if [[ "$choice" -ge 0 && "$choice" -lt ${#files[@]} ]]; then + update_last_modified_at "${files[choice]}" + "$EDITOR_COMMAND" "${files[$choice]}" +else + echo "Invalid selection." + exit 1 +fi -- cgit v1.2.3