Instantly share code, notes, and snippets.

@herrkaefer herrkaefer/newnote.sh
Last active Oct 18, 2016

Embed
What would you like to do?
#!/bin/bash
# Add a new note (format: markdown with Jekyll-like front matter)
# Usage: addnote.sh title category
# Author: herrkaefer (gloolar@gmail.com)
# Root directory of notes
notesdir="/Users/herrk/Dropbox/Notes"
# title is required
if [ -z "$1" ]
then
echo "No title specified. Usage: addnote.sh title category"
exit 1
fi
title=$1
filename=$1.md
# If category is not specified, it is "uncategorized"
if [ -z "$2" ]
then
echo "No category specified. Note will be put into uncategorized directory."
category="uncategorized"
# Create uncategorized subfolder if it does not exist
if [ ! -d "$notesdir/""$category" ]
then
mkdir "$notesdir/""$category"
fi
else
category=$2
fi
# New category subfolder is forced to be created manually to avoid misspelling
if [ ! -d "$notesdir/""$category" ]
then
echo "Specified category does not exist. If you are sure, make a category subfolder manually first."
exit 1
fi
file="$notesdir/""$category/""$filename"
echo "title: "$title
echo "file: ""$file"
echo "category: "$category
# Open note if it exists
if [ -f "$file" ]
then
echo "Note exists."
open "$file"
exit 1
fi
# Create new note file and fill with initial front matter
touch "$file"
echo --- >> "$file"
echo layout: post >> "$file"
echo title: $title >> "$file"
echo category: $category >> "$file"
echo tags: >> "$file"
echo date: $(date +%Y-%m-%d) >> "$file"
echo last: $(date +%Y-%m-%d) >> "$file"
echo published: false >> "$file"
echo comments: true >> "$file"
echo summary: >> "$file"
echo --- >> "$file"
open "$file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment