#!/bin/bash # Copyright (c) 2023-2024 Amin Bandali # # Copying and distribution of this file, with or without modification, # are permitted in any medium without royalty provided the copyright # notice and this notice are preserved. This file is offered as-is, # without any warranty. set -eu filename= title= usage() { printf "Usage: $0 [filename] [title]\n" } usage() { cat <&2 usage 1 ;; esac done read filename title <<<"$@" # Prompt for filename if it was not provided as an arg. while [ -z "$filename" ]; do read -erp "filename: " filename done # Exit with error if file already exists. if [ -e "$filename" ]; then printf "$0: error: file $filename already exists\n" exit 1 fi # Check if we have a generate function for the file extension, # and drop out if not. ext="${filename##*.}" if ! type generate_"$ext" 2>/dev/null | \ grep -q "generate_$ext is a function"; then printf "\ $0: error: no generate function for extension $ext please define a function named generate_$ext to handle creation of new $ext files\n" exit 1 fi # Prompt for title if it was not provided as an arg. while [ -z "$title" ]; do read -erp "title: " title done # Run the generate function, which would normally generate the # output and write it to a new file with the given name. generate_"$ext" # Open the newly created file in editor. $EDITOR "$filename" } generate_txt() { cat > "$filename" < "$filename" < $title - bandali

$title

$(date +'%a, %-d %b %Y')

TODO: write :-)

EOF } main "$@"