courses/imposter-video/hands-on-creating-a-useful-shell-script
● 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.

Buy the course$79
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.

#!/bin/bash
# a Jekyll post creator, which creates a new file, adds frontmatter,
# and opens the editor and starts Jekyll
new_post() {
  JEKYLL_ROOT=~/Documents/Sites/conery-io-jekyll
  JEKYLL_POSTS=$JEKYLL_ROOT/_posts
  TITLE=$1
  SLUGIFIED="$(echo -n "$TITLE" | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z)"
  NEW_POST_FILE=$JEKYLL_POSTS/$(date +%Y-%m-%d-$SLUGIFIED.md)

  cat <<frontmatter > $NEW_POST_FILE
---
-minimal
title: "$TITLE"
image: ''
comments: false
categories:
summary: ""
---
frontmatter

  echo "New post created, opening in Atom, starting Jekyll"
  atom $NEW_POST_FILE
  jekyll serve -s $JEKYLL_ROOT
}