How to Add Better Logging to Bash Scripts
If I were to look for a site reliability / infrastructure / devops engineer job one of the skills that will likely be mentioned across a large number of specs would be “scripting”. Next to it, you’ll likely see the word “bash”.
It is probably the scripting language that I’ve used most frequently, both professionally and personally, in that time I’ve found a few ways to make my scripts a little more robust and user friendly.
What’s a Bash Script?
Before we go any further, we should tackle the question of what a bash script actually is. Now, there is a lot of documentation out there which will explain things better than I could, but for the purposes of this post, I like to think of bash scripts as:
A plain text file containing any series of commands that can be run on the command line.
It’s probably worth noting that in order to execute any of the commands contained
within the file, you need to ensure your file has the execute permission enabled,
this can be achieved pretty simply with
chmod +x <file_name>
When writing scripts it’s always a good idea to add some safeguards to ensure that an error in an earlier command does not cascade through the script resulting in unexpected behaviour. Assuming that appropriate safeguards have been added, I’ve found including a few helper functions to handle logging can go a long way where user experience is concerned.
Below is a small snippet I like to add to my scripts
It sets up some colour variables, and then creates a few functions which can
be used later in the script to
echo outputs to the end user in the colour(s) specified.
I’ve settled on the use of
RED as they do a good
job of communicating, errors, warnings, prompts and transitions between commands,
however, you can always add or change the colours by updating the colour codes
Below I’ve included a sample script showing how my snippet works in practice:
And with that, thanks for reading.