Tagged: bash

A simple markdown blog using three lines of bash

Imagine you’ve got a load of markdown files such as post1.md etc.

And that you have a command, markdown, that converts markdown to html. (This exists: look it up).

This will convert all your markdown files to HTML and put them in a directory called tmp which you will have to create.

for i in `ls *.md`; do    markdown $i > tmp/`basename $i .md`.html; done

Now this next command will surround all your new HTML files with a <div class='entry'> tag and output the whole lot into entries.html.

ls tmp/* | xargs -n 1 awk 'BEGIN {print "<div class=\"entry\">" } {print $0} END {print "</div>"}' > entries.html

Finally create a template.html file with the lines <!-- insert here --> somewhere in the middle.

Then this final command will insert everything in entries.html into your template.html file and output the result into a new file, index.html

sed -e '/<!-- insert here/r entries.html' template.html > index.html
unix bash

Bash command line movement and deletion shortcuts

If you have typed in

Man I was mean, but I'm changing my scene

And then press alt shift b (the shift may not be needed) twice the cursor will go back to the m of my.

(alt shift f moves in the opposite direction, incidentally)

Press alt shift d it will delete the word my. Press it again and it will then delete scene.

Press ctl d and you will delete not the word but a single character under the cursor.

Press ctl k and you will delete everything after the position. ctl u everything before.

Press ctl _ and you will undo your command line edits.

unix bash

Bash: functions and arguments

A function is easily defined in a script as somename() { echo "blar" } which you then call via somename.

You can pass parameters like somename "hi there" and with the definition somename() { echo ${1}!! }.


Bash: case statement

The case statement is simple enough, and notibily works with regular expressions:

  # Uses a regex
  #anything else

esac is case backwards.

unix unix-bash

Using inotifywait with a while loop in bash

Install inotify-tools and then you can wait for a file to change using:

inotifywait -m thefile.txt

The -m flag monitors the file, instead of exiting on the first event. You can use -e to wait for a particular event–see the man page.

But you probably want to do something when that happens. You can pipe the output of the command to a bash while loop:

inotifywait -m thefile.txt | while read file; do echo $file; done

file in this case will have the file name and event name, but you don’t need to use it obviously.

bash bash-while inotifywait unix

Page 1 of 2