123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696 |
- ##############################################################################
- #
- # Library Functions
- #
- ##############################################################################
- init () {
- #
- # Debug mode shows more verbose output to screen and log files.
- # Value: yes or no (y / n)
- #
- DEBUG=no
- #
- # Syslog style log messages
- #
- if ! defined LOGDATEFORMAT
- then
- LOGDATEFORMAT="%b %e %H:%M:%S"
- fi
- if ! defined LOG_FILE
- then
- LOG_FILE=$0.log
- fi
- #
- # Enable / disable logging to a file
- # Value: yes or no (y / n)
- #
- if ! defined LOG_ENABLED
- then
- LOG_ENABLED=no
- fi
- if ! defined SYSLOG_ENABLED
- then
- SYSLOG_ENABLED=no
- fi
- if ! defined SYSLOG_TAG
- then
- SYSLOG_TAG=$0
- fi
- #
- # Use colours in output.
- #
- RED="tput setaf 1"
- GREEN="tput setaf 2"
- YELLOW="tput setaf 3"
- BLUE="tput setaf 4"
- MAGENTA="tput setaf 5"
- CYAN="tput setaf 6"
- LIGHT_BLUE="$CYAN"
- BOLD="tput bold"
- DEFAULT="tput sgr0"
- RED_BG="tput setab 1"
- GREEN_BG="tput setab 2"
- YELLOW_BG="tput setab 3"
- BLUE_BG="tput setab 4"
- MAGENTA_BG="tput setab 5"
- CYAN_BG="tput setab 6"
- #
- # Bug fix for Bash, parsing exclamation mark.
- #
- set +o histexpand
- #
- # returns 0 if a variable is defined (set)
- # returns 1 if a variable is unset
- #
- }
- function defined {
- if [ -n "$ZSH_VERSION" ]; then
- [ -n "${1+X}" ]
- else
- [[ ${!1-X} == ${!1-Y} ]]
- fi
- }
- function is_function {
- if [ -n "$(type -t $1)" ] && [ "$(type -t $1)" = function ]; then
- return 0
- else
- return 1
- fi
- }
- function is_alias {
- if [ -n "$(type -t $1)" ] && [ "$(type -t $1)" = 'alias' ]; then
- return 0
- else
- return 1
- fi
- }
- #
- # returns 0 if a variable is defined (set) and value's length > 0
- # returns 1 otherwise
- #
- function has_value {
- if [ -n "$ZSH_VERSION" ]; then
- # if defined $1; then
- # if [[ -n ${!1} ]]; then
- # return 0
- # fi
- # fi
- return 1
- else
- if defined $1; then
- if [[ -n ${!1} ]]; then
- return 0
- fi
- fi
- return 1
- fi
- }
- #
- # returns 0 if a directory exists
- # returns 1 otherwise
- #
- function directory_exists {
- if [[ -d "$1" ]]; then
- return 0
- fi
- return 1
- }
- #
- # returns 0 if a (regular) file exists
- # returns 1 otherwise
- #
- function file_exists {
- if [[ -f "$1" ]]; then
- return 0
- fi
- return 1
- }
- function command_exists {
- if command -v "$1" > /dev/null 2>&1; then
- return 0;
- fi
- return 1
- }
- #
- # returns lowercase string
- #
- function tolower {
- echo "$1" | tr '[:upper:]' '[:lower:]'
- }
- #
- # returns uppercase string
- #
- function toupper {
- echo "$1" | tr '[:lower:]' '[:upper:]'
- }
- #
- # Only returns the first part of a string, delimited by tabs or spaces
- #
- function trim {
- echo $1
- }
- #
- # Dummy function to provide usage instructions.
- # Override this function if required.
- #
- show_usage () {
- MESSAGE="$1"
- echo "$MESSAGE"
- exit 1
- }
- #
- # Checks if a variable is set to "y" or "yes".
- # Usefull for detecting if a configurable option is set or not.
- #
- option_enabled () {
- VAR="$1"
- VAR_VALUE=$(eval echo \$$VAR)
- if [[ "$VAR_VALUE" == "y" ]] || [[ "$VAR_VALUE" == "yes" ]]
- then
- return 0
- else
- return 1
- fi
- }
- #
- # The log funcion just puts a string into a file, prepended with a date & time in
- # syslog format.
- #
- log2syslog () {
- if option_enabled SYSLOG_ENABLED
- then
- MESSAGE="$1"
- logger -t "$SYSLOG_TAG" " $MESSAGE" #The space is not a typo!"
- fi
- }
- #
- # This function writes messages to a log file and/or syslog
- # The only argument is a message that has to be logged.
- #
- log () {
- if option_enabled LOG_ENABLED || option_enabled SYSLOG_ENABLED
- then
- LOG_MESSAGE="$1"
- DATE=`date +"$LOGDATEFORMAT"`
- if has_value LOG_MESSAGE
- then
- LOG_STRING="$DATE $LOG_MESSAGE"
- else
- LOG_STRING="$DATE -- empty log message, no input received --"
- fi
- if option_enabled LOG_ENABLED
- then
- echo "$LOG_STRING" >> "$LOG_FILE"
- fi
- if option_enabled SYSLOG_ENABLED
- then
- #
- # Syslog already prepends a date/time stamp so only the message
- # is logged.
- #
- log2syslog "$LOG_MESSAGE"
- fi
- fi
- }
- #
- # This function basically replaces the 'echo' function in bash scripts.
- # The added functionality over echo is logging and using colors.
- #
- # The first argument is the string / message that must be displayed.
- # The second argument is the text color.
- msg () {
- MESSAGE="$1"
- COLOR="$2"
- if ! has_value COLOR
- then
- COLOR="$DEFAULT"
- fi
- if has_value "MESSAGE"
- then
- $COLOR
- echo "$MESSAGE"
- $DEFAULT
- log "$MESSAGE"
- else
- echo "-- no message received --"
- log "$MESSAGE"
- fi
- }
- #
- # This function echos a message
- # and displays the status at the end of the line.
- #
- # It can be used to create status messages other
- # than the default messages available such as
- # OK or FAIL
- #
- msg_status () {
- MESSAGE="$1"
- STATUS="$2"
- msg "$MESSAGE"
- display_status "$STATUS"
- }
- #
- # These functions are just short hand for messages like
- # msg_status "this message is ok" OK
- #
- #
- # The following functions are shorthand for
- # msg_status "a message" OK
- # msg_status "another message" FAIL
- msg_emergency () {
- MESSAGE="$1"
- STATUS="EMERGENCY"
- msg_status "$MESSAGE" "$STATUS"
- }
- msg_alert () {
- MESSAGE="$1"
- STATUS="ALERT"
- msg_status "$MESSAGE" "$STATUS"
- }
- msg_critical () {
- MESSAGE="$1"
- STATUS="CRITICAL"
- msg_status "$MESSAGE" "$STATUS"
- }
- msg_error () {
- MESSAGE="$1"
- STATUS="ERROR"
- msg_status "$MESSAGE" "$STATUS"
- }
- msg_warning () {
- MESSAGE="$1"
- STATUS="WARNING"
- msg_status "$MESSAGE" "$STATUS"
- }
- msg_notice () {
- MESSAGE="$1"
- STATUS="NOTICE"
- msg_status "$MESSAGE" "$STATUS"
- }
- msg_info () {
- MESSAGE="$1"
- STATUS="INFO"
- msg_status "$MESSAGE" "$STATUS"
- }
- msg_debug () {
- MESSAGE="$1"
- STATUS="DEBUG"
- msg_status "$MESSAGE" "$STATUS"
- }
- msg_ok () {
- MESSAGE="$1"
- STATUS="OK"
- msg_status "$MESSAGE" "$STATUS"
- }
- msg_not_ok () {
- MESSAGE="$1"
- STATUS="NOT_OK"
- msg_status "$MESSAGE" "$STATUS"
- }
- msg_fail () {
- MESSAGE="$1"
- STATUS="FAILED"
- msg_status "$MESSAGE" "$STATUS"
- }
- msg_success () {
- MESSAGE="$1"
- STATUS="SUCCESS"
- msg_status "$MESSAGE" "$STATUS"
- }
- msg_passed () {
- MESSAGE="$1"
- STATUS="PASSED"
- msg_status "$MESSAGE" "$STATUS"
- }
- check_status () {
- CMD="$1"
- STATUS="$2"
- if [ "$STATUS" == "0" ]
- then
- msg_ok "$CMD"
- else
- msg_fail "$CMD"
- fi
- }
- #
- # Private function
- #
- # This is a function that just positions
- # the cursor one row up and to the right.
- # It then prints a message with specified
- # Color
- # It is used for displaying colored status messages on the
- # Right side of the screen.
- #
- # ARG1 = "status message (OK / FAIL)"
- # ARG2 = The color in which the status is displayed.
- #
- raw_status () {
- STATUS="$1"
- COLOR="$2"
- function position_cursor () {
- let RES_COL=`tput cols`-12
- tput cuf $RES_COL
- tput cuu1
- }
- position_cursor
- echo -n "["
- $DEFAULT
- $BOLD
- $COLOR
- echo -n "$STATUS"
- $DEFAULT
- echo "]"
- log "Status = $STATUS"
- }
- #
- # This function converts a status message to a particular color.
- #
- display_status () {
- STATUS="$1"
- case $STATUS in
- EMERGENCY )
- STATUS="EMERGENCY"
- COLOR="$RED"
- ;;
- ALERT )
- STATUS=" ALERT "
- COLOR="$RED"
- ;;
- CRITICAL )
- STATUS="CRITICAL "
- COLOR="$RED"
- ;;
- ERROR )
- STATUS=" ERROR "
- COLOR="$RED"
- ;;
- WARNING )
- STATUS=" WARNING "
- COLOR="$YELLOW"
- ;;
- NOTICE )
- STATUS=" NOTICE "
- COLOR="$BLUE"
- ;;
- INFO )
- STATUS=" INFO "
- COLOR="$LIGHT_BLUE"
- ;;
- DEBUG )
- STATUS=" DEBUG "
- COLOR="$DEFAULT"
- ;;
- OK )
- STATUS=" OK "
- COLOR="$GREEN"
- ;;
- NOT_OK)
- STATUS=" NOT OK "
- COLOR="$RED"
- ;;
- PASSED )
- STATUS=" PASSED "
- COLOR="$GREEN"
- ;;
- SUCCESS )
- STATUS=" SUCCESS "
- COLOR="$GREEN"
- ;;
- FAILURE | FAILED )
- STATUS=" FAILED "
- COLOR="$RED"
- ;;
- *)
- STATUS="UNDEFINED"
- COLOR="$YELLOW"
- esac
- raw_status "$STATUS" "$COLOR"
- }
- #
- # Exit with error status
- #
- bail () {
- ERROR="$?"
- MSG="$1"
- if [ ! "$ERROR" = "0" ]
- then
- msg_fail "$MSG"
- exit "$ERROR"
- fi
- }
- #
- # This function executes a command provided as a parameter
- # The function then displays if the command succeeded or not.
- #
- cmd () {
- COMMAND="$1"
- msg "Executing: $COMMAND"
- RESULT=`$COMMAND 2>&1`
- ERROR="$?"
- MSG="Command: ${COMMAND:0:29}..."
- tput cuu1
- if [ "$ERROR" == "0" ]
- then
- msg_ok "$MSG"
- if [ "$DEBUG" == "1" ]
- then
- msg "$RESULT"
- fi
- else
- msg_fail "$MSG"
- log "$RESULT"
- fi
- return "$ERROR"
- }
- #
- # These functions can be used for timing how long (a) command(s) take to
- # execute.
- #
- now () {
- echo $(date +%s)
- }
- elapsed () {
- START="$1"
- STOP="$2"
- echo $(( STOP - START ))
- }
- #
- # Prints an error message ($2) to stderr and exits with the return code ($1).
- # The message is also logged.
- #
- function die {
- local -r err_code="$1"
- local -r err_msg="$2"
- local -r err_caller="${3:-$(caller 0)}"
- msg_fail "ERROR: $err_msg"
- msg_fail "ERROR: At line $err_caller"
- msg_fail "ERROR: Error code = $err_code"
- exit "$err_code"
- } >&2 # function writes to stderr
- #
- # Check if a return code ($1) indicates an error (i.e. >0) and prints an error
- # message ($2) to stderr and exits with the return code ($1).
- # The error is also logged.
- #
- # Die if error code is false.
- #
- function die_if_false {
- local -r err_code=$1
- local -r err_msg=$2
- local -r err_caller=$(caller 0)
- if [[ "$err_code" != "0" ]]
- then
- die $err_code "$err_msg" "$err_caller"
- fi
- } >&2 # function writes to stderr
- #
- # Dies when error code is true
- #
- function die_if_true {
- local -r err_code=$1
- local -r err_msg=$2
- local -r err_caller=$(caller 0)
- if [[ "$err_code" == "0" ]]
- then
- die $err_code "$err_msg" "$err_caller"
- fi
- } >&2 # function writes to stderr
- #
- # Replace some text inside a string.
- #
- function str_replace () {
- local ORIG="$1"
- local DEST="$2"
- local DATA="$3"
- echo "$DATA" | sed "s/$ORIG/$DEST/g"
- }
- #
- # Replace string of text in file.
- # Uses the ed editor to replace the string.
- #
- # arg1 = string to be matched
- # arg2 = new string that replaces matched string
- # arg3 = file to operate on.
- #
- function str_replace_in_file () {
- local ORIG="$1"
- local DEST="$2"
- local FILE="$3"
- has_value FILE
- die_if_false $? "Empty argument 'file'"
- file_exists "$FILE"
- die_if_false $? "File does not exist"
- printf ",s/$ORIG/$DEST/g\nw\nQ" | ed -s "$FILE" > /dev/null 2>&1
- return "$?"
- }
- #
- # Prompt a user and if the response is as expected return 0, else 1
- #
- # arg1 = the prompt for the user
- # arg2 = the regex to match against ('yY' default)
- #
- function prompt_user {
- EXPECTED=${2:-yY}
- echo -n "$1($EXPECTED)"
- read REPLY
- echo
- if [[ ! $REPLY =~ ^[$EXPECTED]$ ]]
- then
- return 1
- else
- return 0
- fi
- }
- #
- # Prompt a user to remove a file
- #
- # arg1 = the file to remove
- #
- function remove_file_with_prompt {
- if file_exists $1; then
- if ! prompt_user "Remove exsting $1 file?"; then
- echo "Move $1 and rerun"
- return 1
- fi
- echo "Removing $1"
- rm $1
- fi
- }
- init
|