● videoCore Skills
Hands On: Creating a Useful Shell Script

Free for everyone
This video is free for everyone
Watch the whole thing, no account needed. If it clicks, grab CS Fundamentals on Video for lifetime access to every lesson.
SECTION
Core Skills
NEXT UP
Deciphering a Complex Bash Script
COURSE
Computer Science Fundamentals
34 lessons
About this lesson
I use the static site generator Jekyll to write my blog. I store the site at Github, who then translates and hosts it all for me for free. Jekyll is simple to use and I like it a lot. There's only one problem: it's a bit manual.
Every post that you create has to have a very specific filename in the form of year-month-day-post-slug.md. Not that big of a problem, just a mere annoyance. In addition, every post you have must have a blob of text at the top called the front matter. Again: not a huge problem, just kind of time consuming. The perfect candidate for a little help from a shell script.
<span class="hljs-meta">#!/bin/bash</span>
<span class="hljs-comment"># a Jekyll post creator, which creates a new file, adds frontmatter,</span>
<span class="hljs-comment"># and opens the editor and starts Jekyll</span>
<span class="hljs-function"><span class="hljs-title">new_post</span></span>() {
JEKYLL_ROOT=~/Documents/Sites/conery-io-jekyll
JEKYLL_POSTS=<span class="hljs-variable">$JEKYLL_ROOT</span>/_posts
TITLE=<span class="hljs-variable">$1</span>
SLUGIFIED=<span class="hljs-string">"<span class="hljs-subst">$(echo -n <span class="hljs-string">"<span class="hljs-variable">$TITLE</span>"</span> | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)</span>"</span>
NEW_POST_FILE=<span class="hljs-variable">$JEKYLL_POSTS</span>/$(<span class="hljs-built_in">date</span> +%Y-%m-%d-<span class="hljs-variable">$SLUGIFIED</span>.md)
<span class="hljs-built_in">cat</span> <<<span class="hljs-string">frontmatter > $NEW_POST_FILE
---
-minimal
title: "$TITLE"
image: ''
comments: false
categories:
summary: ""
---
frontmatter</span>
<span class="hljs-built_in">echo</span> <span class="hljs-string">"New post created, opening in Atom, starting Jekyll"</span>
atom <span class="hljs-variable">$NEW_POST_FILE</span>
jekyll serve -s <span class="hljs-variable">$JEKYLL_ROOT</span>
}