Skip to content

Instantly share code, notes, and snippets.

@digitaljhelms
Created June 17, 2021 02:26
Show Gist options
  • Save digitaljhelms/115b95f9eca619e4a07f9514aa8eca21 to your computer and use it in GitHub Desktop.
Save digitaljhelms/115b95f9eca619e4a07f9514aa8eca21 to your computer and use it in GitHub Desktop.
Include files for bash scripts
#!/bin/bash
# check if stdout is a terminal
if [ -t 1 ]; then
# see if it supports colors
ncolors=$(tput colors)
if [[ -n "$ncolors" && $ncolors -ge 8 ]]; then
bold="$(tput bold)"
underline="$(tput smul)"
standout="$(tput smso)"
normal="$(tput sgr0)"
black="$(tput setaf 0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
blue="$(tput setaf 4)"
magenta="$(tput setaf 5)"
cyan="$(tput setaf 6)"
white="$(tput setaf 7)"
fi
fi
#!/bin/bash
# place this file within the main directory containing other bash
# scripts, or in a child directory of the main directory, and
# add the following line to those scripts:
# source "${BASH_SOURCE%/*}/path/to/this/file"
#
# if the color script exists in the same directory as this file it will
# automatically be sourced and color output will be used
#
# Function := error
# Usage := error "This is the error reason."
# @param: <optional>
# @Stdout: "! Aborting. This is the error reason."
#
# if no parameter is sent when error is called, "No reason provided."
# will be used as the default reason reported to stdout
if [ -f "${BASH_SOURCE%/*}/color" ]; then
source "${BASH_SOURCE%/*}/color"
fi
function error {
reason=$1
if [ -z "$reason" ]; then
reason="No reason provided."
fi
echo -e "${bold}${red}${standout}!${normal} ${bold}${red}Aborting.${normal} ${bold}$reason${normal}" >&2
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment