RSS Feed Share on Twitter

All Shell Scripting Tips

20 Aug 2018

Banner

A standardised banner function for better presentation

When writing a script which produces a lot of output, it can be useful to have a standardised banner or header function, which formats a heading for each section in an easily-identified, clear and standardised way.

An installer, or any script which does a set of configuration tasks would be obvious candidates. If you have a script which runs as part of a Jenkins (or similar tool's) build steps, the sections can be easily delineated with a standardised banner.

This doesn't need to be anything complicated; a little bit of ASCII Art is all that is needed, at most.

download this script (banner.sh)
#!/bin/bash

banner()
{
  echo "+------------------------------------------+"
  printf "| %-40s |\n" "`date`"
  echo "|                                          |"
  printf "|`tput bold` %-40s `tput sgr0`|\n" "$@"
  echo "+------------------------------------------+"
}

banner "Starting the Job"
sleep 3

banner "Copying files"
cp -v /etc/hosts /tmp
cp -v /etc/passwd /tmp
sleep 4

banner "Downloading article"
curl https://www.shellscript.sh/tips/banner/ > /tmp/banner.html
sleep 5

banner "Finished."

When run, this should look something like this:

This is of course a very simplistic example, but as you can see, the main code body is easy to follow, and the output from the script is far more clear than if you just got the output from the cp and curl commands:

This trivial modification to your script can make a surprisingly significant difference to the way the script is percieved; by just passing a short description of the current status to the banner() function, the script is much easier to use and to debug.

Formatting notes

You can use whatever formatting works best for you; this example uses printf and tput.

tput enables basic formatting within a terminal; here, we use tput bold to enable bold text, and tput sgr0 to set the terminal back to default settings.

There are various utilities with the name printf; within a shell script, this is usually printf(1), which is normally a shell builtin command. It takes similar arguments to the C language printf and other implementations. Here, we use the "%-40s" syntax to tell printf to pad out the message to make it 40 characters wide. That way, the final "|" symbol lines up properly with the rest of the box:

  printf "| %-40s |\n" "`date`"

Footnote

Many Unix flavours had a banner utility, which would make large text out of its arguments, like this:

Solaris $> banner Hello
#     #
#     #  ######  #       #        ####
#     #  #       #       #       #    #
#######  #####   #       #       #    #
#     #  #       #       #       #    #
#     #  #       #       #       #    #
#     #  ######  ######  ######   ####

Solaris $> 

 

 


You can buy the content of this tutorial as a PDF to download to all of your devices!

Contact

You can mail me with this form. If you expect a reply, please ensure that the address you specify is valid. Don't forget to include the simple addition question at the end of the form, to prove that you are a real person!