blog.johanka.net

The Anatomy of a Bash Script

Readers of this blog have no doubt already noticed that many Bash scripts contain the following pattern

#!/bin/bash

if [...] ; then

...

fi

exit 0

or a variation thereof.

It is often placed in a file that ends with the extension .sh and is chmodded 700 (to make it executable).

So if, for instance, I want to create a really simple script to delete all the unnecessary hidden files from my USB stick, I will create a new .sh file, make it executable like this

chmod 700 USBcleanup.sh

and add this code into it:

#!/bin/bash

if [ -d /full/path/to/USB ] ; then

cd /full/path/to/USB

rm ._* && echo "Files with the name matching this pattern have been deleted successfully." || echo "There are no files of this name."

fi

exit 0

You'll get the full path by typing in the command pwd in the same directory.

The part in the square brackets starting with -d is there to check if the directory exists, and the two vertical lines || basically mean that you've instructed the system to display the following message if the previous command could not have been executed (simply because there were no files matching the pattern in this folder).

You can then run this script by typing

./USBcleanup.sh

in the Terminal from the directory where the .sh file is located. You can even create a shortcut by adding a line to your .bashrc file, for instance

alias usbdel="full/path/to/USBcleanup.sh"

You will then be able to type usbdel from anywhere in the directory structure to start this script.

Let us now build upon the pattern shown above and create a slightly modified version of the basic structure.

#!/bin/bash

if [ $date "+%w" = 1 ] ; then

cd $HOME/Desktop && xed weekly.txt || echo "Your task list for this week hasn't been created yet."

else

cd $HOME/Desktop && head -n 3 weekly.txt > today.txt && echo "Have a nice day!" >> today.txt && cat today.txt

fi

exit 0

The if statement at the beginning simply means, if today is the first day in the week (Monday) do the following: Go to the location specified ($HOME is a variable that stands for user's home directory) and open the file named weekly.txt. In all other cases go to the same folder, take the first three lines from the file of the same name, add them to the file named today.txt with a line saying "Have a nice day!" and display the content of the file in the Terminal. You can again alias this script and open it with the shortcut MO or ag (for agenda), for instance. If you type in man date in the Terminal, you will get the full list of options you can use to replace %w at the beginning, or add a new if section. Feel free to play with it, it's amazing how much fun can be had with a simple script like this.

Have fun breaking down your new year's resolution into doable monthly or weekly chunks. This kind of automation can, of course, be brought even a step further, more on this later.

If you haven't yet switched to something more adventurous, you will certainly want to check out the latest Mint upgrade. The brightest among you are, of course, already happily hacking away at Debian, which was actually my goal. :-) Keep it up. I will definitely continue this blog in the following year. Since I also play with everything I write about, I will probably keep the pace of two new articles every month or so.

copy left Johanka Piskovská, 2020