Skip to content

Instantly share code, notes, and snippets.

@JoeyBurzynski
Last active September 18, 2020 10:57
Show Gist options
  • Save JoeyBurzynski/36502ba94f4b28e83c7182549dee1198 to your computer and use it in GitHub Desktop.
Save JoeyBurzynski/36502ba94f4b28e83c7182549dee1198 to your computer and use it in GitHub Desktop.
Bash / Shell Script: Ensure Minimum Bash Version is Met
#!/usr/bin/env bash
# = Bash / Shell Script: Ensure Minimum Bash Version is Met
# ? File Name: version.check.sh
# * Echo message, prefixed with timestamp.
# * Optionally, supports color name as argument to alter color of output.
function echoc () {
# * If no message has been passed, echo an empty line.
if [ -z "$1" ]; then echo; return; fi
# * Color Codes
# = Regular Bold Underline High Intensity BoldHigh Intens Background High Intensity Backgrounds
# ^ Black='\e[0;30m'; BoldBlack='\e[1;30m'; UnderlineBlack='\e[4;30m'; IntenseBlack='\e[0;90m'; BoldInstenseBlack='\e[1;90m'; On_Black='\e[40m'; On_IntenseBlack='\e[0;100m';
# ^ Red='\e[0;31m'; BoldRed='\e[1;31m'; UnderlineRed='\e[4;31m'; IntenseRed='\e[0;91m'; BoldInstenseRed='\e[1;91m'; On_Red='\e[41m'; On_IntenseRed='\e[0;101m';
# ^ Green='\e[0;32m'; BoldGreen='\e[1;32m'; UnderlineGreen='\e[4;32m'; IntenseGreen='\e[0;92m'; BoldInstenseGreen='\e[1;92m'; On_Green='\e[42m'; On_IntenseGreen='\e[0;102m';
# ^ Yellow='\e[0;33m'; BoldYellow='\e[1;33m'; UnderlineYellow='\e[4;33m'; IntenseYellow='\e[0;93m'; BoldInstenseYellow='\e[1;93m'; On_Yellow='\e[43m'; On_IntenseYellow='\e[0;103m';
# ^ Blue='\e[0;34m'; BoldBlue='\e[1;34m'; UnderlineBlue='\e[4;34m'; IntenseBlue='\e[0;94m'; BoldInstenseBlue='\e[1;94m'; On_Blue='\e[44m'; On_IntenseBlue='\e[0;104m';
# ^ Purple='\e[0;35m'; BoldPurple='\e[1;35m'; UnderlinePurple='\e[4;35m'; IntensePurple='\e[0;95m'; BoldInstensePurple='\e[1;95m'; On_Purple='\e[45m'; On_IntensePurple='\e[0;105m';
# ^ Cyan='\e[0;36m'; BoldCyan='\e[1;36m'; UnderlineCyan='\e[4;36m'; IntenseCyan='\e[0;96m'; BoldInstenseCyan='\e[1;96m'; On_Cyan='\e[46m'; On_IntenseCyan='\e[0;106m';
# ^ White='\e[0;37m'; BoldWhite='\e[1;37m'; UnderlineWhite='\e[4;37m'; IntenseWhite='\e[0;97m'; BoldInstenseWhite='\e[1;97m'; On_White='\e[47m'; On_IntenseWhite='\e[0;107m';
# * Color Codes
local colorReset='\e[0m' # * Color Reset
local black='\e[1;30m'
local red='\e[1;31m'
local green='\e[1;32m'
local yellow='\e[1;33m'
local blue='\e[1;34m'
local purple='\e[1;35m'
local cyan='\e[1;36m'
local white='\e[1;37m'
local activeColor
local message
local timestamp
# * Generate timestamp
timestamp="$(date +'%Y-%m-%d / T%H:%M:%S%z')"
# * Message passed.
message="$1"
# * If color name is provided, attempt to use that.
# * Else, default to white.
activeColor=${!2:-$white}
# * Echo message with timestamp and optional color code.
# ? Then you can just echo -e "${Blue}blue ${Red}red ${ResetColor}etc...."
echo -e "${black}$timestamp: ${activeColor}$message${colorReset}"
}
# * Ensure minimum bash version requirement is met.
# ? Bash version 4 or greater supports associative arrays.
function ensureBashCompatbility () {
# * Define the minimum support Bash version.
local minimum_bash_version=6
echoc 'Ensuring Bash version compatibility.' 'yellow'
echoc " - Minimum Bash Version Required: $minimum_bash_version"
# * Exit unless Bash version requirement is met.
if [[ ! "${BASH_VERSINFO:-0}" -ge "$minimum_bash_version" ]]; then
echoc
echoc " ⚠ Fatal Error (Bash Version): This script requires Bash v$minimum_bash_version or greater to execute." red
echoc " Bash Version (Current): $BASH_VERSION" red
echoc
exit 1
else
echoc " ✓ Bash Version: $BASH_VERSION" "green"
echoc
echoc
fi
}
ensureBashCompatbility
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment