Post

Automating Jekyll Post Creation with a Bash Script

I’m going to be updating this page whenever I change my script and pasting the updated version below this block of text which will shift down the previous version of my script every time I update it.

As of June 9th I think I may have finished the script and may not update it unless I come up with another ease-of-access feature.

Version 1.0 of my script (I went to Reddit and asked random people who knew more about bash than I do how to get an editor to open and they provided efficiency fixes as well.) The script opens the file after creation at the end, uses a heredoc instead of printf and echo, there is a new variable called targetfile to probably make it more efficient, and just in case the title variable is already set the script now unsets it at runtime which prevents a rare case where a title might already be passed into the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash

#getting date and time
date=$(date +'%Y-%m-%d')
time=$(date +'%H:%M:%S')
#clear title
unset title
#loop to ensure input is received
boolean=0
until [[ -n "${title}" ]] ; do
        read -r -p 'Input post title: ' title
done

#lowercase title variable
lowertitle=$(echo $title | tr '[:upper:]' '[:lower:]')
#changing spaces to '-' characters
dashtitle=$(echo $lowertitle | tr ' ' '-')
#set target file name
targetfile="_posts/$date-$dashtitle.md"
#adding the front matter
cat >> "$targetfile" <<EOF
---
title: "$title"
date: $date $time
categories: []
tags: []
---


EOF

#opening new file in whatever editor
"${EDITOR:-vim}" "${targetfile}"

Version 0.2 of my script (Added missing header function and fixed front matter. Still missing automatic opening of the file after it’s created):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

#getting date and time
date=$(date +'%Y-%m-%d')
time=$(date +'%H:%M:%S')
#getting title and checking if input is received
boolean=0
while [ "$boolean" -ne 1 ]; do
        read -p 'Input post title: ' title
        if [ -z "${title}" ]
        then
                echo "title cannot be blank"
        else
                boolean=1
        fi
done

#lowercase title variable
lowertitle=$(echo $title | tr '[:upper:]' '[:lower:]')
dashtitle=$(echo $lowertitle | tr ' ' '-')
#Creating the file and adding "---" at the top since printf thinks I'm trying to add arguments when I use "--" after it.
echo "---" >> _posts/$date-$dashtitle.md
#Adding rest of the front matter
printf "title: \"$title\"\ndate: $date $time -0400\ncategories: []\ntags: []\n---\n\n# $title\n\n\n" >> _posts/$date-$dashtitle.md
#opening newly created file in vim
#vim _posts/$date-$dashtitle.md

Version 0.1 of my script (Missing function to add header below the front matter which will almost always just be the title of the post as well as a function that automatically opens the newly created file at the end of the scripts execution. Also prints a newline before the front matter which makes it so it’s not read and is instead treated like a paragraph block when converted to HTML and completely defeats the purpose of this script. The reason I put the newline initually was because the printf command didn’t like the dashes after the quotes which are the start of the front matter):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash

#getting date and time
date=$(date +'%Y-%m-%d')
time=$(date +'%H:%M:%S')
#getting title and checking if input is received
boolean=0
while [ "$boolean" -ne 1 ]; do
        read -p 'Input post title: ' title
        if [ -z "${title}" ]
        then
                echo "title cannot be blank"
        else
                boolean=1
        fi
done

#lowercase title variable
lowertitle=$(echo $title | tr '[:upper:]' '[:lower:]')
dashtitle=$(echo $lowertitle | tr ' ' '-')
#creating file and adding front matter
printf "\n---\ntitle: \"$title\"\ndate: $date $time -0400\ncategories: []\ntags: []\n---" >> _posts/$date-$dashtitle.md
#opening newly created file in vim
#vim _posts/$date-$dashtitle.md
This post is licensed under CC BY 4.0 by the author.