How to Write a Shell Script?

2019-07-01 by terryoy, in guides

Finnally, I've done it! Wow, it took me 3 hours to explore the ability of bash scripts.

I was trying to write a shell script to help me create this blog article, in an interactive way.

The blog articles are organized in a directory structure: _content/{year}/{article-name-slug.md}, and it also has a template to write the meta info for this article, which is like:

# How to Write a Shell Script?
- date: 2019-07-01 18:11
- tags: shell
- category: guides
-------

So the basic idea is like this:

  • I want to create the page where I could simply input the title, tags, and category
  • the date time is generated by the script
  • the file path is determined by the information I input and the date
  • the file name is slugified
  • all the information is filled in the template
  • use vim to open the file and filled the initial content with the template

All the functions I needed and searched are listed below:

  • prompt info and read values from user input. using echo and read command
  • list the tags and categories. using ls -p, tr, sort, and column
  • making the steps as functions
  • fill template file with environmental variables. using cat, echo and eval
  • creatign temporary file to hold the generated content from the template. using $RANDOM env variable for a random number.
  • fill the vi editing content after entering the editor. using vi <filename> -c "read <tmpfile>"

So the final example of the shell script can be found here


Tags: shell