From aa00529d3af69add6243898c314d0b68c0085bc2 Mon Sep 17 00:00:00 2001 From: Ian Leonard Date: Fri, 19 Oct 2018 17:20:54 +0000 Subject: [PATCH] config/functions: add die() function die is meant to be a more flexible exit, printing a message to go with the exit call. It works like: die -- same as "exit 1" die "It just broke." -- echo statement, exit value 1 die "Tried to do the thing." "2" -- echo statement, exit value 2 Exit codes other than 1 require a message to go with it. it would replace existing code that looks like: echo "ERROR: Everything went wrong. Sorry!" exit 1 or be more helpful in picking up the pieces when something unexpected happens then: do_something || exit 1 Signed-off-by: Ian Leonard --- config/functions | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/config/functions b/config/functions index 58364d45ce..aa40401a68 100644 --- a/config/functions +++ b/config/functions @@ -1,4 +1,12 @@ ### FUNCTION HELPERS ### +# die (message, code) abort with optional message and code +die() { + if [ -n "$1" ]; then + echo -e "$1" >&2 + fi + exit "${2:-1}" +} + # return 0 if $2 in space-separated list $1, otherwise return 1 listcontains() { if [ -n "$1" -a -n "$2" ]; then