R language could be easily used as a bash script using Rscript *.R
. system()
is a R base function which could run command line within R.
Below is a simple example which allows to automate create a new blog post: (1) Ask users to type in filename, title and language (2) Create a new markdown file in specific directory (i.e. your local posts saved path) (3) Add some metadata in .md file (4) Open the file using your favorite markdown editor.
'Your filename > ')
cat(<- scan(file = "stdin", what = "character", n=1, sep = "-")
filename
"Your post's title > ")
cat(<- scan("stdin", what = "character", n=1, sep = "-")
title
"language is zh or en > ")
cat(<- scan("stdin", character(), n=1)
lang
= TRUE
draft
=
md.metadata "---
paste0(title: ", title,"
",Sys.Date(),"
date: ", tolower(draft) ,"
draft:
categories:- blog
tags:- Blog
---")
if (lang == "zh"){
## create a file name
= paste0(Sys.Date(), "-",gsub(" ", "-", filename, fixed = TRUE), "." ,lang,".md")
postname ## Chinese Path
= paste0(
filepath "/Users/jihong/Documents/hugo-academic-jihong/content/post/zh/",
postname)
= paste0("cd /Users/jihong/Documents/hugo-academic-jihong/content/post/zh/ && open ", postname)
cmd else {
} = paste0(Sys.Date(), "-",gsub(" ", "-", filename, fixed = TRUE), ".md")
postname ## English Path
= paste0(
filepath "/Users/jihong/Documents/hugo-academic-jihong/content/post/",
postname)
= paste0("cd /Users/jihong/Documents/hugo-academic-jihong/content/post/ && open ", postname)
cmd
}
file = filepath, quote = FALSE, row.names = FALSE,col.names = FALSE)
write.table(md.metadata,
system(cmd)