bash_shell

Automate everything (practical uses of Bash part 2)

I started this series with some quick time-savers. The goal of automation is saving time and providing focus on important things rather than tedious yak shaving. The time it takes to write a little script is hardly anything compared to the time it saves.

So here are some more useful tips including how to share functions across scripts, check for environment variables, output in color, and generalize checksum verification.

Output in color
You can output text in color using the -e flag on the echo command and ANSI color codes.

For example, to print “Hello World” in bold white text:

boldwhitehelloworld.sh

#!/bin/bash
echo -e "\n \033[1m Hello World \033[0m \n"

Or to fail a script with bold red text:

boldredfailuremessage.sh

#!/bin/bash
echo -e "\033[1;31m \n The script has failed \e[00m\n"
exit 1

You can find more on your own by searching for “bash ansi color codes.”

Share functions across scripts
If you like printing things out in color, you might want to do it in more than one script. And you probably don’t want to copy/paste the same functions into multiple scripts over and over again. No problem. Use the source command to pull in what you need.

First, put your functions in a script.

functions.sh

#!/bin/bash
function failmsg {
  echo -e "\033[1;31m \n $1 \e[00m\n"
}

function greenmsg {
  echo -e "\033[1;32m \n $1 \e[00m \n"
}

function boldwhitemsg {
  echo -e "\n \033[1m $1 \033[0m \n"
}

Next, source it in another:

helloworld.sh

#!/bin/bash

source functions.sh

greenmsg "Hello World"

And another:

boldredfailuremessage.sh

#!/bin/bash

source functions.sh

failmsg "The script has failed"

Check for environment variables
We all know what happens when we assume. Something that frequently gets assumed in shell scripts is the existence of environment variables.

There’s two ways to check for them. One is to do something like env | grep MY_VARIABLE and see if there’s a result, but since you can refer to environment variables directly from a shell script, you can write a function like this.

checkforenv.sh

#!/bin/bash

source functions.sh

function checkForEnvVariable {
  valueOfVariable=${!1}
  if [ ! "$(echo $valueOfVariable)" ]; then
    failmsg "A required environment variable is not available: $1"
    exit 1
  fi
}

# check for necessary env variables
checkForEnvVariable WEBAPP_HOME

If you don’t have a $WEBAPP_HOME environment variable defined, this will abort the script with the message:

A required environment variable is not available: WEBAPP_HOME

Generalize file checksum verification
Let’s pretend that you’re going download and install VirtualBox. Once you get to the VirtualBox download page, it tells you you can compare your download with some MD5 or SHA-256 checksums.

Once you have two files you need (in my case, since I’m running Fedora 26, I have VirtualBox-5.1-5.1.26_117224_fedora25-1.x86_64.rpm and SHA256SUMS) in the same directory, you can execute the following command to verify that the download is correct:

sha256sum -c SHA256SUMS --ignore-missing

Great! Seems so easy I’m not sure that I need a script.

Later, I download Tomcat. Uh oh! Apache uses MD5 for their checksums. I download the two files. Hmm…the syntax for MD5 verification is super similar to SHA-256:

md5sum -c apache-tomcat-8.5.20.tar.gz.md5 --ignore-missing

I bet we can work out a way to download any two files to the same directory, and run a single script that can verify the MD5 or SHA-256 checksum of a file.

#!/bin/bash

source functions.sh

hashfile=$1
checksumtype=$2

checksum=`"$checksumtype"sum -c $hashfile --ignore-missing`

greenmsg "$checksum"

Have fun cutting out the yak shaving and I’m sure I’ll be back with another installment soon.

One thought on “Automate everything (practical uses of Bash part 2)

Leave a Reply

Your email address will not be published. Required fields are marked *