Skip to content

Instantly share code, notes, and snippets.

@Rud5G
Last active June 13, 2022 08:44
Show Gist options
  • Save Rud5G/b8a1057b4c1a57590773f368cbd936af to your computer and use it in GitHub Desktop.
Save Rud5G/b8a1057b4c1a57590773f368cbd936af to your computer and use it in GitHub Desktop.
Common collection (bash)
PROFILE=sandbox
#!/bin/bash
set -e
# see https://gist.github.com/Rud5G/b8a1057b4c1a57590773f368cbd936af
# include with:
# . $(dirname "$0")/common.sh
# Under normal circumstances, you shouldn't need to change anything below this line.
# -----------------------------------------------------------------------------
export PATH="$PATH:/usr/bin:/usr/local/bin"
# If true, all messages will be printed. If false, only fatal errors are printed.
DEBUG=${DEBUG:-true}
# Usage: msg <message>
#
# Writes <message> to STDERR.
msg() {
local message="$1"
echo "$message" 1>&2 || true
}
# Usage: debugmsg <message>
#
# Writes <message> to STDERR only if $DEBUG is true, otherwise has no effect.
debugmsg() {
local message="$1"
($DEBUG && msg "$message") || true
}
# Usage: error_exit <message>
#
# Writes <message> to STDERR as a "fatal" and immediately exits the currently running script.
error_exit() {
local message="$1"
echo "[FATAL] $message" 1>&2
exit 1
}
read_dotenv_exports() {
local dotenvfile="${1:-.env}"
local dir="$(pwd)"
set -o allexport
source "$dir/$dotenvfile"
set +o allexport
}
#!/bin/bash
. $(dirname "$0")/common.sh
#
# output of running this file must be:
# example
# output 2
# [FATAL] exiting now
#
msg "example"
DEBUG=false
debugmsg "no output"
DEBUG=true
debugmsg "output 2"
error_exit "exiting now"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment