Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
1
reputation
2

BadSegway

☕ null

Free badge offered; thereforce, I shall insert pointless things here, sooooo:

u haz a bash script coloring library?

#!/bin/bash
# -------------------------------------------
# Simple functions for making tasty logging scripts ;)
# ~ @BadSegway, 2019 - MIT License (if anyone cares/asks)
# https://gitlab.com/snippets/1838117/raw
# -------------------------------------------

# Set some functions for logging in color
color()     { echo -e "[\033[0;35m$(date -u)\033[0m] \033[1;${1}m${@:2}\033[0m"; }
success()   { color 32 "SUCCESS: ${@}"; }
warn()      { color 33 "WARN:    ${@}"; }
comment()   { color 36 "COMMENT: ${@}"; }
err()       { color 31 "ERROR:   ${@}"; exit 1; }
ctrlc()     { echo ""; err "CTRL-C pressed; quitting ..."; }
info()      { color 37 "INFO:    ${@}"; }

# Exit on error
trap 'ctrlc' SIGINT
trap 'err "line $LINENO; exitting ..."' ERR

# -------------------------------------------
# If you run this file with "demo" as an
# argument, the below demo code will run
# -------------------------------------------
if [[ $# -gt 0 && $1 = "demo" ]]; then

SCRIPTUSER="${USER}"
SCRIPTPATH="${BASH_SOURCE}"

sudo -u "${SCRIPTUSER}" bash -s <<EOF
    # Load awesome coloring ;)
    source "${SCRIPTPATH}"

    # Used like:
    success Something went right
    warn    Something may have went wrong
    comment Something is just a comment
    info    Something is normal

    # We can type a command wrong (and we'll auto exit due
    # to the ERR trap we set in the utils script above)
    # Try running:
    # $ some_command_that_shouldnt_exist_and_will_throw_an_error

    # We can also explicitly exit when something goes wrong
    # but doesn't throw an exit code error
    err     'Something went wrong (we did it ;)) quitting'
EOF
echo ""
fi
  • Atlanta, GA, USA
  • gitlab.com/badsegway
  • Member for 30 days
  • 0 profile views
  • Last seen Apr 6 at 23:36

Communities (1)

Top Network Posts

We respect a laser-like focus on one topic.

Keeping a low profile.

This user hasn't posted yet.

Badges (2)

Gold

Silver

Bronze

2

Rarest