#!/usr/bin/env bash set -o pipefail if [ -z "$1" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then echo 'Usage: scripts/mkdraft.sh Title Of Your Post' [ -z "$1" ] && exit 1 || exit fi # thx https://stackoverflow.com/a/29436423 function yes_or_no { read -p "$* [y/N]: " yn case $yn in [Yy]*) return 0 ;; *) echo "Aborted" ; return 1 ;; esac } title="$@" path="src/posts/drafts/$(date +%F)-$(echo -n $title | tr 'A-Z' 'a-z' | tr -c '[:alnum:]' - | sed -E 's|-+|-|g').md" if [ -f $path ]; then yes_or_no "$path already exists! Do you want to overwrite it?" || exit fi cat << EOF > $path --- title: $title --- # $title EOF echo "Created new draft in $path"