# This file is meant to be sourced by another script, # it contains only BASH functions and global variables. ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## lib.sh ## ## ## ## Library for my personal BASH scripts ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## Information ## (don't touch unless you are me) ## LIB_NAME="lib.sh" LIB_DESCRIPTION="Library for my personal BASH scripts" LIB_VERSION="0.1.0" LIB_AUTHOR="david@socialnerds.org" LIB_LICENSE="MIT" LIB_WEBSITE="https://git.socialnerds.org/david/scripts" ## ## Variables ## # Text formatting if [[ -t 1 ]]; then LIB_CLEAR="\e[0m" # Text settings LIB_BOLD="\e[1m" LIB_UNDERLINE="\e[4m" # Text color LIB_RED="\e[31m" LIB_GREEN="\e[32m" LIB_YELLOW="\e[33m" LIB_BLUE="\e[34m" LIB_MAGENTA="\e[35m" LIB_CYAN="\e[36m" LIB_LIGHTGREY="\e[37m" # Text color with bold font LIB_RED_BOLD="\e[1;31m" LIB_GREEN_BOLD="\e[1;32m" LIB_YELLOW_BOLD="\e[1;33m" LIB_BLUE_BOLD="\e[1;34m" LIB_MAGENTA_BOLD="\e[1;35m" LIB_CYAN_BOLD="\e[1;36m" LIB_LIGHTGREY_BOLD="\e[1;37m" # Background color LIB_RED_BG="\e[41m" LIB_GREEN_BG="\e[42m" LIB_YELLOW_BG="\e[43m" LIB_BLUE_BG="\e[44m" LIB_MAGENTA_BG="\e[45m" LIB_CYAN_BG="\e[46m" LIB_LIGHTGREY_BG="\e[47m" # Background color with bold font LIB_RED_BG_BOLD="\e[1;41m" LIB_GREEN_BG_BOLD="\e[1;42m" LIB_YELLOW_BG_BOLD="\e[1;43m" LIB_BLUE_BG_BOLD="\e[1;44m" LIB_MAGENTA_BG_BOLD="\e[1;45m" LIB_CYAN_BG_BOLD="\e[1;46m" LIB_LIGHTGREY_BG_BOLD="\e[1;47m" fi ## ## Functions ## # Function aliases #function lib_message() { lib_print "$@"; } function lib_checks() { lib_healthchecks $@; } # Print text message function lib_print() { local LIB_Q=${Q:-0} local LIB_V=${V:-0} if [[ "$1" =~ ^\! ]]; then printf "$LIB_RED_BG_BOLD%b$LIB_CLEAR\n" "${1#\!}" elif [[ "$1" =~ ^\? ]]; then if [ $LIB_V -eq 1 ] && [ $LIB_Q -ne 1 ]; then printf "$LIB_LIGHTGREY%b$LIB_CLEAR\n" "${1#\?}" fi elif [[ $LIB_Q -ne 1 ]]; then printf "%b\n" "$1" fi } # Print script information # including the last 5 changelog entries function lib_print_info() { printf "%12s %b\n%12s %s\n%12s %s\n%12s %s\n%12s %s\n%12s %s\n\n%12s %s\n" \ "Name:" "$LIB_BOLD$NAME$LIB_CLEAR" "Version:" "$VERSION" \ "Author:" "$AUTHOR" "License:" "$LICENSE" "Web:" "$WEBSITE" \ "Description:" "$DESCRIPTION" "Changelog:" "${CHANGELOG[0]}" for i in "${CHANGELOG[@]:1:4}"; do printf "%12s %s\n" "" "$i" done } # Print version information function lib_print_version() { printf "%s\n" "$VERSION" } # Check for privileges # Returns 0 if the current user is root and 1 if not function lib_amiroot() { if [[ $(whoami) != "root" ]]; then return 1 fi } # Check for command availability # Takes one or a list of space seperated commands. Returns a list of missing commands. function lib_missing_commands() { local MISSING_COMMANDS="" for COMMAND in $@; do if ! $(command -v $COMMAND &>/dev/null); then MISSING_COMMANDS="$MISSING_COMMANDS $COMMAND" fi done echo "${MISSING_COMMANDS# }" } # Generate a random alphanumeric string # Optional: Supply length as intager. Default is 16. function lib_gen_string() { LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c ${1:-16}; echo } # Contact Healthchecks # The first argument($1) must be the URL everything else gets transmitted as message function lib_healthchecks() { if [[ "$2" ]] && lib_is_url "$1"; then curl -fsS -m 10 --retry 5 --data-raw "${*:2}" -o /dev/null "$1" elif [[ "$1" ]] && lib_is_url "$1"; then curl -fsS -m 10 --retry 5 -o /dev/null "$1" fi } # Send a notification to ntfy.sh # The first argument($1) must be the URL everything else gets transmitted as message function lib_ntfy() { MESSAGE=${*:2} if [[ "$2" ]] && lib_is_url "$1"; then if [[ "$2" =~ ^\! ]]; then curl -fsS -m 10 --retry 5 -H "Title: ${NAME:-lib_ntfy}" -H "Tags: red_circle" -d "${MESSAGE#\!}" -o /dev/null "$1" else curl -fsS -m 10 --retry 5 -H "Title: ${NAME:-lib_ntfy}" -H "Tags: green_circle" -d "$MESSAGE" -o /dev/null "$1" fi fi } # Verify if input is an intager function lib_is_int { local INT_EXPR='^[0-9]+$' if ! [[ "$1" =~ $INT_EXPR ]]; then return 1 fi } # Verify if input is alphanumeric function lib_is_alnum { local ALNUM_EXPR='^[a-zA-Z0-9]+$' if ! [[ "$1" =~ $ALNUM_EXPR ]]; then return 1 fi } # Verify if input looks like an URL function lib_is_url { local URL_EXPR='^https?://.+$' if ! [[ "$1" =~ $URL_EXPR ]]; then return 1 fi } # Verify if input is an existing directory function lib_is_dir { if ! [[ -d "$1" ]]; then return 1 fi } # Verify if input is an absolute path function lib_is_absolute { if ! [[ "$1" == /* ]]; then return 1 fi } # Verify if input is a relative path function lib_is_relative { if [[ "$1" == /* ]]; then return 1 fi } # Verify if PID (still) exists function lib_is_running { ps -p $1 > /dev/null if [[ $? -ne 0 ]]; then return 1 fi }