Skip to content

Instantly share code, notes, and snippets.

@slowpeek
Last active June 4, 2022 00:52
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slowpeek/6127166369d8abd230c30c20cc6a9152 to your computer and use it in GitHub Desktop.
Save slowpeek/6127166369d8abd230c30c20cc6a9152 to your computer and use it in GitHub Desktop.
bye.sh
# -*- mode: sh; sh-shell: bash; -*-
# shellcheck shell=bash
# MIT license (c) 2021 https://github.com/slowpeek
# Homepage: https://gist.github.com/slowpeek/6127166369d8abd230c30c20cc6a9152
##############################################################
##############################################################
## THIS GIST IS OBSOLETE AND IS NO LONGER UPDATED.
## Use here-bye instead: https://github.com/slowpeek/here-bye
##############################################################
##############################################################
# IN SHORT
#
# Print a message and exit. 'set -eu' friendly.
#
# DETAILS
#
# Notice the difference:
#
# $ ./demo.sh
# Something is wrong
#
# $ BYE_PREFIX=auto BYE_VERBOSE=y ./demo.sh
# [./demo.sh:81 helper_func] Something is wrong
#
# Call stack:
# ./demo.sh:81 helper_func
# ./demo.sh:85 do_something
#
# Default behaviour:
#
# - join arguments with a single space to form a message
# - print the message
# - exit 1
#
# Variables:
#
# - BYE_PREFIX
# - BYE_EXIT
# - BYE_VERBOSE
#
# Configuration:
#
# The message can be optinally prefixed with context:
#
# [prefix] message
#
# The prefix can be set with BYE_PREFIX. A special value 'auto' causes
# it to take such form:
#
# lineno:file funcname
#
# _funcname_ is there if _bye_ was called from a function.
#
# Custom exit code can be set with BYE_EXIT.
#
# With BYE_VERBOSE=y call stack is printed after the message if _bye_
# was called from a function.
bye () {
IFS=' '
{
local lvl=${#FUNCNAME[@]} lineno func file
# Only print not empty messages.
if (($# > 0)); then
local prefix=${BYE_PREFIX-}
if [[ $prefix == auto ]]; then
read -r lineno func file < <(caller 0)
prefix="$file:$lineno"
((lvl <= 2)) || prefix+=" $func"
fi
[[ -z $prefix ]] || prefix="[$prefix] "
printf "%s%s\n" "$prefix" "$*"
fi
if [[ ${BYE_VERBOSE-} == y ]] && ((lvl > 2)); then
local s n=0 stack=()
while s=$(caller "$n"); do
read -r lineno func file <<< "$s"
((++n))
stack+=("$file:$lineno $func")
done
unset -v 'stack[-1]'
echo -e '\nCall stack:'
printf '%s\n' "${stack[@]}"
fi
} >&2
exit "${BYE_EXIT:-1}"
}
@matthewpersico
Copy link

This is cool. A suggestion:

starting at line 59:

local lvl=${#FUNCNAME[@]} lineno func file warn=0
if [[ $1 =~ -warn ]]; then
    warn=1; shift
fi

then at line 91:

if (( warn )) ; then
    return "${BYE_EXIT:-1}"
else
    exit "${BYE_EXIT:-1}" 
}

warn ()
{
    bye --warn "$@"
}

That way you get nice warning messages too.

@slowpeek
Copy link
Author

@matthewpersico Hey! Sry for slow response :)

I've finally reworked it into this https://github.com/slowpeek/here-bye

Now it is a standalone context aware printer and an exit function. You can use here or (here2 instead of here >&2) function to print messages with context and bye to print a message and exit. Welcome to the README!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment