diff options
author | uvok cheetah | 2024-12-18 17:39:22 +0100 |
---|---|---|
committer | uvok cheetah | 2024-12-18 17:39:22 +0100 |
commit | f273bcdcf7acb7c69f1ae279d9f20a8b58726f06 (patch) | |
tree | 1c883ef35fe1e955524c0a5576d9f762b3cb9aba | |
parent | 05b4da4026658b4744156ea0f03e753ca84ce9f2 (diff) |
-rw-r--r-- | editpost.sh | 49 |
1 files changed, 49 insertions, 0 deletions
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 <pattern>" + 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 |