3 # ax-common.sh -- Common Functions for Shell Scripts
4 # Copyright (c)2013-2017 Alexander Barton (alex@barton.de)
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
12 # "ax_common_sourced" is a flag and stores the "API version" of "ax-common.sh",
13 # too. It should be incremented each time the public API changes!
14 # shellcheck disable=SC2034
17 # Display a colored message (a plain message, when not writing to a terminal).
18 # $1 Level: -=title, 0=ok, 1=warning, 2=error.
19 # $2 Word(s) to highlight.
20 # $3-n Remaining word(s). [optional]
23 # writing to a terminal ...
25 "0") c="32"; shift; ;; # green
26 "1") c="33"; shift; ;; # yellow
27 "2") c="31"; shift; ;; # red
28 "-") c="1"; shift; ;; # bold
31 # print colored word(s):
32 printf "\033[0;%sm%s\033[0m " "${c}" "${1}"
38 # print remaining word(s) and trailing newline:
42 # Display a colored message.
43 # $1 Level, see ax_msg1 function.
44 # $2-n Word(s) to highlight.
51 # Display an error message to stderr.
52 # [-l] Log message to syslog, too.
55 if [ "$1" = "-l" ]; then
57 if [ -n "$NAME" ]; then
58 logger -t "$NAME" "$*"
66 # Abort the script with an error message and exit code 1.
67 # [-l] Log message to syslog, too.
68 # $1 Error message [optional]. Will be formatted as "Error: %s Aborting!".
69 # if no error message is given, "Aborting!" will be printed.
71 if [ "$1" = "-l" ]; then
78 ax_error $log_param "Error: $* Aborting!"
80 ax_error $log_param "Aborting!"
85 # Display a debug message, when debug mode is enabled, that is, the environment
86 # variable "DEBUG" is set.
88 [ -n "$DEBUG" ] && ax_msg1 1 "DEBUG:" "$*"