20 Aug 2018
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/examples/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.
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`"
Many Unix flavours had a banner
utility, which would make large text out of its arguments, like this:
Solaris $> banner Hello # # # # ###### # # #### # # # # # # # ####### ##### # # # # # # # # # # # # # # # # # # # # ###### ###### ###### #### Solaris $>
My Shell Scripting books, available in Paperback and eBook formats. This tutorial is more of a general introduction to Shell Scripting, the longer Shell Scripting: Expert Recipes for Linux, Bash and more book covers every aspect of Bash in detail.
![]() Shell Scripting Tutorial is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and in paperback format good to keep by your desk as an ever-present companion. Also available in PDF form from Gumroad:Get this tutorial as a PDF | ![]() Shell Scripting: Expert Recipes for Linux, Bash and more is my 564-page book on Shell Scripting. The first half covers all of the features of the shell in every detail; the second half has real-world shell scripts, organised by topic, along with detailed discussion of each script. |