diff options
-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 |