| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# Copyright (C) 2012-2020 SUSE Software Solutions Germany GmbH |
| 4 |
# |
| 5 |
# Author: |
| 6 |
# Frank Sundermeyer <fsundermeyer at opensuse dot org> |
| 7 |
# |
| 8 |
# |
| 9 |
function print_help { |
| 10 |
cat <<EOF_helptext |
| 11 |
Conditional Color echo |
| 12 |
|
| 13 |
Usage: ccecho "LEVEL" "MESSAGE" |
| 14 |
|
| 15 |
Prints a colored message string to STDOUT or STDERR using bash color codes. The |
| 16 |
color and the output channel depend on the LEVEL keyword. When the SHELL |
| 17 |
environment variable COLOR is set to either "0" or "no" no color output is |
| 18 |
generated. |
| 19 |
|
| 20 |
The main purpose of this script is to provide an easy method to generate |
| 21 |
colored output when the output should go to STDOUT, and regular output |
| 22 |
(with no color codes messing up the output) for log files, pipes and |
| 23 |
scripts (by exporting COLOR=0) |
| 24 |
|
| 25 |
|
| 26 |
LEVEL keywords |
| 27 |
normal -> no color, STDOUT |
| 28 |
error -> bold red, STDERR |
| 29 |
info -> purple, STDOUT |
| 30 |
result -> green, STDOUT |
| 31 |
warn -> red, STDERR |
| 32 |
|
| 33 |
MESSAGE |
| 34 |
The message to be printed. Needs to be quoted using double quotes. |
| 35 |
You may use sequences supported by echo (1) such as \n to insert a |
| 36 |
newline and \t to insert a tab. |
| 37 |
EOF_helptext |
| 38 |
} |
| 39 |
|
| 40 |
#---------------------------------------------------------------------- |
| 41 |
# printing help / catching errors |
| 42 |
# |
| 43 |
ME=$(basename "$0") |
| 44 |
|
| 45 |
ARGS=$(getopt -o h -l help -n "$ME" -- "$@") |
| 46 |
eval set -- "$ARGS" |
| 47 |
while true ; do |
| 48 |
case "$1" in |
| 49 |
-h|--help) |
| 50 |
print_help |
| 51 |
exit |
| 52 |
shift |
| 53 |
;; |
| 54 |
--) |
| 55 |
shift |
| 56 |
break |
| 57 |
;; |
| 58 |
esac |
| 59 |
done |
| 60 |
|
| 61 |
if [[ -z "$1" || -z "$2" ]]; then |
| 62 |
echo "ERROR: $ME must be called with two parameters: |
| 63 |
LEVEL and MESSAGE |
| 64 |
See '$ME -h' or 'man 1 $ME' for more information" >&2 |
| 65 |
exit 1 |
| 66 |
fi |
| 67 |
|
| 68 |
#---------------------------------------------------------------------- |
| 69 |
# MAIN |
| 70 |
# |
| 71 |
LEVEL="$1" |
| 72 |
MESSAGE="$2" |
| 73 |
|
| 74 |
START_COLOR="" |
| 75 |
END_COLOR="" |
| 76 |
|
| 77 |
# set colors according to level if colored output is wanted |
| 78 |
# |
| 79 |
if [[ 0 != "$COLOR" && no != "$COLOR" ]]; then |
| 80 |
END_COLOR="\e[0m" |
| 81 |
if [[ error = "$LEVEL" ]]; then |
| 82 |
START_COLOR="\e[1;31m" |
| 83 |
elif [[ info = "$LEVEL" ]]; then |
| 84 |
START_COLOR="\e[35m" |
| 85 |
elif [[ result = "$LEVEL" ]]; then |
| 86 |
START_COLOR="\e[32m" |
| 87 |
elif [[ warn = "$LEVEL" ]]; then |
| 88 |
START_COLOR="\e[31m" |
| 89 |
fi |
| 90 |
# in case you do a tail or a head on the output, the last/first color code |
| 91 |
# might get cut, leading to unwanted results |
| 92 |
# To be on the safe side, we make sure every line that is printed starts |
| 93 |
# and ends with a color code |
| 94 |
# (\ from \n needs to be escaped) |
| 95 |
# |
| 96 |
MESSAGE=${MESSAGE//\\n/${END_COLOR}\\n${START_COLOR}} |
| 97 |
fi |
| 98 |
|
| 99 |
if [[ error = "$LEVEL" || warn = "$LEVEL" ]]; then |
| 100 |
echo -e "${START_COLOR}${MESSAGE}${END_COLOR}" >&2 |
| 101 |
else |
| 102 |
echo -e "${START_COLOR}${MESSAGE}${END_COLOR}" |
| 103 |
fi |