1# Date
2DATE=`date +%Y-%m-%d`
This will get today’s date in YYYY-MM-DD format.
1$ str="This is just a test"
2$ echo ${str// /_}
3This_is_just_a_test
What’s cooler than a bash script? A bash function! With a function, we don’t have to specify script name/location every time. A bash function added to your .bash_profile
will work globally anywhere in the Terminal. The command to run would then become like this:
1jpost "My new post"
To make the script a function, all you have to do is wrap the code in functionname() { }
like so:
1jpost() {
2 # code goes here
3}
It’s be nice if you could define options for your script and specify them at runtime. Like -t
for Type and -l
for Layout. Maybe a -o
for Opening file in Sublime Text after it has been created.
So the command would become something like this:
1jpost -t=markdown -l=post -o "My new post"
We can do this with the bash builtin getopts
. Define our options like so:
1while getopts "o" opt; do
2 case $opt in
3 o) open=1 ;;
4 esac
5done
And then modify our if statements accordingly. For example, if -o
is provided, open the file in Sublime Text
1# check for -o (open) argument
2if [ ! -z $open ]; then
3 createPost
4 # Open file in Sublime Text
5 open -a "Sublime Text" $FILEPATH
6fi
A simple if
statement can make sure that you are not overwriting existing files.
1if (-e $FILENAME ); then
2 echo "File already exists!"
3else
4 // Run our code
5fi
-e
is the operator that checks if a file exists. -s
is the operator that checks if file exists and is not empty.
A simple success or error message about the results of our command would be nice. What would be even nicer if they were red or green based on their run status.
I want it to say “File has been successfully created” (green text) when a file has been cerated without any errors. If a file already exists, it should say “File already exists!” (red text).
Let’d define some colors and use them in our script.
1# COLORS
2Color_Off='\033[0m' # Text Reset
3Red='\033[0;31m' # Red
4Green='\033[0;32m' # Green
5Yellow='\033[0;33m' # Yellow
6
7echo -e "${Green}File was succesfully CREATED${Color_Off}"
8echo -e "${Red}File already EXISTS and is NOT EMPTY${Color_Off}"
9echo -e "${Yellow}File already EXISTS${Color_Off}"
If you’d like to open all new files in your favorite editor after they are created, you can add that too. The following is what i have to open all post files in Sublime Text:
1open -a "Sublime Text" $FILEPATH
You should add this right after the file is created in your code.
#!/bin/bash | |
# About: Bash script to create new Jekyll posts | |
# Author: @AamnahAkram | |
# URL: https://gist.github.com/aamnah/f89fca7906f66f6f6a12 | |
# Description: This is a more advanced version of the script which can | |
# - take options | |
# - has color coded status messages | |
# - improved code | |
# - lowercase permalinks | |
# - usage message | |
# - opens file in Sublime Text after creation | |
# VARIABLES | |
###################################################### | |
# COLORS | |
Color_Off='\033[0m' # Text Reset | |
# Regular Colors | |
Black='\033[0;30m' # Black | |
Red='\033[0;31m' # Red | |
Green='\033[0;32m' # Green | |
Yellow='\033[0;33m' # Yellow | |
Blue='\033[0;34m' # Blue | |
Purple='\033[0;35m' # Purple | |
Cyan='\033[0;36m' # Cyan | |
White='\033[0;37m' # White | |
jpost() { | |
# VARIABLES | |
########### | |
# Define the post directory (where to create the file) | |
JEKYLL_POSTS_DIR=$HOME'/Sandbox/jekyll/_posts/' | |
# Post title | |
# if more than one argument is provided then TITLE is the last argument | |
if [ $# -gt 1 ]; then | |
TITLE=${@: -1} | |
# if only one argument is provided then it is the Title | |
else | |
TITLE=$1 | |
fi | |
# Replace spaces in title with underscores | |
TITLE_STRIPPED=${TITLE// /_} | |
# Permalink | |
PERMALINK=$(tr A-Z a-z <<< $TITLE_STRIPPED) | |
# Date | |
DATE=`date +%Y-%m-%d` | |
# Post Type (markdown, md, textile) | |
TYPE='.md' | |
# File name structure | |
FILENAME=${DATE}-${TITLE_STRIPPED}${TYPE} | |
# File path | |
FILEPATH=${JEKYLL_POSTS_DIR}${FILENAME} | |
# USAGE | |
########### | |
showUsage() { | |
echo "Usage: jpost -o \"This is my post\" " | |
} | |
# CREATE POST | |
############# | |
createPost() { | |
# create file | |
touch ${FILEPATH} | |
# add YAML front matter to the newly created file | |
echo -e "--- | |
layout: post | |
title: ${TITLE} | |
permalink: ${PERMALINK} | |
--- | |
" > ${FILEPATH} | |
# success message | |
echo -e "${Green}File was succesfully CREATED \n | |
${JEKYLL_POSTS_DIR}${FILENAME}${Color_Off} | |
" | |
} | |
# ARGUMENTS | |
########### | |
while getopts "o" opt; do | |
case $opt in | |
o) open=1 ;; | |
esac | |
done | |
# CONDITIONS | |
############ | |
if [ $# -eq 0 ]; then | |
showUsage | |
else | |
# check if file alreday exists and is not empty | |
if [ -s ${FILEPATH} ]; then | |
echo -e "${Red}File already EXISTS and is NOT EMPTY${Color_Off}" | |
#check if file already exists | |
elif [ -e ${FILEPATH} ]; then | |
echo -e "${Yellow}File already EXISTS${Color_Off}" | |
# check for -o (open) argument | |
elif [ ! -z $open ]; then | |
createPost | |
# Open file in Sublime Text | |
open -a "Sublime Text" $FILEPATH | |
# if no file with the same name exists, proceed with creating a new one | |
else | |
createPost | |
fi | |
fi | |
} |
#!/bin/bash | |
# About: Bash script to create new Jekyll posts | |
# Author: @AamnahAkram | |
# URL: https://gist.github.com/aamnah/f89fca7906f66f6f6a12 | |
# Description: This is a very basic version of the script | |
# VARIABLES | |
###################################################### | |
# Get current working directory | |
# Define the post directory (where to create the file) | |
JEKYLL_POSTS_DIR=$HOME'/Sandbox/jekyll/_posts/' | |
# Post title | |
TITLE=$1 | |
# Replace spaces in title with underscores | |
TITLE_STRIPPED=${TITLE// /_} | |
# Permalink | |
PERMALINK=${TITLE_STRIPPED} | |
# Date | |
DATE=`date +%Y-%m-%d` | |
# Post Type (markdown, md, textile) | |
TYPE='.md' | |
# File name structure | |
FILENAME=${DATE}-${TITLE_STRIPPED}${TYPE} | |
# COMMANDS | |
####################################################### | |
# go to _posts directory | |
cd ${JEKYLL_POSTS_DIR} | |
# make a new post file | |
touch ${FILENAME} | |
# add YAML front matter | |
echo -e " | |
--- | |
layout: post | |
title: ${TITLE} | |
permalink: ${PERMALINK} | |
--- | |
" >> ${FILENAME} | |
} |
with underscores _
: The easiest way to replace white spaces with (underscores) _ in bash