#!/bin/bash #usage: # source fontFormatting.sh # echo -e "${colorForegroundRed}text" #directory of script: # scriptPath=`dirname ${0}` # scriptPathAbs=`realpath ${scriptPath}` commandCurrentDate='if [ "${LOG_USE_DATE}" != "" ]; then echo -n "[";date +%y-%m-%d_%H:%M:%S | tr -d "\n";echo -n "] ";fi' #neccessary for piped calls to get combined error codes set -o pipefail formatReset='\e[0m' colorForegroundDefault='\e[39m' colorForegroundBlack='\e[30m' colorForegroundRed='\e[31m' colorForegroundGreen='\e[32m' colorForegroundYellow='\e[33m' colorForegroundBlue='\e[34m' colorForegroundWhite='\e[97m' colorBackgroundDefault='\e[49m' colorBackgroundBlack='\e[40m' colorBackgroundRed='\e[41m' colorBackgroundGreen='\e[42m' colorBackgroundYellow='\e[43m' colorBackgroundBlue='\e[44m' colorBackgroundWhite='\e[107m' colorError="${colorForegroundWhite}${colorBackgroundRed}" colorWarning="${colorForegroundWhite}${colorBackgroundYellow}" colorSuccess="${colorForegroundWhite}${colorBackgroundGreen}" # info: MATE Terminal has a check box in profile preference to activate bold text. formatBold='\e[1m' formatUnderlined='\e[4m' formatBlink='\e[5m' formatInvertColors='\e[7m' formatHidden='\e[8m' function echoSuccess () { echo -e `eval ${commandCurrentDate}`"${colorSuccess}${1}${formatReset}" } function echoWarning () { echo -e `eval ${commandCurrentDate}`"${colorWarning}${1}${formatReset}" } function echoError () { echo -e `eval ${commandCurrentDate}`"${colorError}${colorForegroundWhite}${1}${formatReset}" } function echoBold () { echo -e `eval ${commandCurrentDate}`"${formatBold}${1}${formatReset}" } function echoNormal () { echo -e `eval ${commandCurrentDate}`"${1}" } #give parameter in one string #correct: runCommand "echo hello world" #wrong: runCommand echo hello world function runCommand () { if [ "${2}" != "" ] then echo -e ${formatUnderlined}${2}${formatReset} fi echo -e "executing: ${colorForegroundGreen}${1}${formatReset}" eval "${1}" } function runCommandInDirectoryExitOnError () { if [ "${1}" == "" ] then #silently ignore command without path return fi if [ "${3}" != "" ] then echo -e "${formatUnderlined}${3}${formatReset}" fi currentPath="${PWD}" runCommandExitOnError "cd ${1}" runCommandExitOnError "${2}" runCommandExitOnError "cd ${currentPath}" } function runCommandExitOnError () { if [ "${1}" == "" ] then #echoError "runCommandExitOnError: got empty command: ${2}" #exit 81 #silently ignore command without path return fi if [ "${2}" != "" ] then echoNormal "${formatUnderlined}${2}${formatReset}" fi echoNormal 'executing: '"${colorForegroundGreen}${1}${formatReset}" eval "${1}" #result=${?} #echo ${PIPESTATUS[@]} result0=${PIPESTATUS[0]} result1=${PIPESTATUS[1]} #echo "debug result: ${result0} ${result1} ?=$? script=${0}" if [ "${result1}" == "" ] then result1="0" fi #echo "debug result: ${result0} ${result1} script=${0}" if [ "${result0}" != "0" ] || [ "${result1}" != "0" ] then #may be called from runCommandInDirectoryExitOnError echoError "Command returned ${result0} ${result1}. Exiting with 82." exit 82 fi } #usage command, variable_name [, information] #e.g. runCommandReturnOutputExitOnError ls lsoutvariablename 'get ls output' function runCommandReturnOutputExitOnError () { if [ "${1}" == "" ] then echoError "runCommandExitOnError: got empty command: ${2}" exit 81 return fi if [ "${3}" != "" ] then echoNormal "${formatUnderlined}${3}${formatReset}" fi echoNormal "executing: ${colorForegroundYellow}${2%Val}=${colorForegroundGreen}${1}${formatReset}" local -n CMD_OUTPUT=${2} CMD_OUTPUT=`eval "${1}"` #result=${?} #echo ${PIPESTATUS[@]} result0=${PIPESTATUS[0]} result1=${PIPESTATUS[1]} #echo "debug result: ${result0} ${result1} ?=$? script=${0}" if [ "${result1}" == "" ] then result1="0" fi #echo "debug result: ${result0} ${result1} script=${0}" if [ "${result0}" != "0" ] || [ "${result1}" != "0" ] then #may be called from runCommandInDirectoryExitOnError echoError "Command returned ${result0} ${result1}. Exiting with 82." exit 82 fi echoNormal "${CMD_OUTPUT}" } function runCommandExitOnSuccess () { if [ "${2}" != "" ] then echo -e "${formatUnderlined}${2}${formatReset}" fi echo -e "executing: ${colorForegroundGreen}${1}${formatReset}" eval "${1}" #eval ${1} #${1} result=${?} if [ "${result}" == "0" ] then echoError "Command returned ${result}. Exiting with exit code 83, since exit requested on success." exit 83 fi } function checkDirectoryAndExitIfNotExists () { if [ ! -d "${1}" ] then if [ "${2}" != "" ] then echoError "ERROR: ${2} does not exist: ${1}" else echoError "ERROR: Directory does not exist: ${1}" fi exit 84 fi if [ "${2}" != "" ] then echoSuccess "OK: ${2} found: ${1}" fi } function checkFileAndExitIfNotExists () { if [ ! -f "${1}" ] then if [ "${2}" != "" ] then echoError "ERROR: ${2} does not exist: ${1}" else echoError "ERROR: File does not exist: ${1}" fi exit 84 fi if [ "${2}" != "" ] then echoSuccess "OK: ${2} found: ${1}" fi } function createDirectoryIfNotExists () { if [ -d "${1}" ] then echoNormal "not creating directory since it exists: ${1}" else commandCreateDir="mkdir ${1}" echoNormal "creating directory: ${commandCreateDir}" ${commandCreateDir} result=${?} if [ "${result}" != "0" ] then echoError "Command returned ${result}. Exiting." exit 85 fi fi } function checkProcessRunning () { if [ "$(ps ax | grep -v grep | grep ${1})" == "" ] then echo "false" else echo "true" fi }