#!/bin/bash ###################################### #Shebang (Unix) for vim formatting # shell script: ?? # PHP: #### load fontFormatting ##################### #scriptOrigin=`readlink -f ${0}` #scriptPath=`dirname ${scriptOrigin}` #scriptPathAbs=`realpath ${scriptPath}` #scriptFontFormatting="${scriptPathAbs}/"'../fontFormatting.sh' #source "${scriptFontFormatting}" #errorCodeFontScript=${?} #if [ "${errorCodeFontScript}" != "0" ] #then # echo "Error when sourcing font script '${scriptFontFormatting}'." # exit ${errorCodeFontScript} #fi #### script startup ########################## #if [ "${1}" == "" ] #then # echoNormal "Hello World" # echo -e "${colorForegroundRed}Hello World!${formatReset}" # echo "usage: ${0} TODO" #fi #### check $PWD agains current path ########## #if [ "$PWD" != "$scriptPathAbs" ] #then # echo 'Script must be run from the path it is located in: '"$scriptPathAbs" # echo '$PWD='"$PWD" # echo 'Using script location path for executtion.' # #echo 'Exiting with error code 2.' # sCMD='cd '"$scriptPathAbs" # echo $sCMD # $sCMD # #exit 2 #fi #### fontlormatting settings ################# # LOG_USE_DATE="1" ############################################# #tips #print textual meaning of error code: perror ${?} ############################################# #useful definitions not used in this script cmdDateSimple="date +%y%m%d" cmdDateTimeSimple="date +%y%m%d_%H%M%S" ############################################# ############################################# #command to print current date and time used in this script 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}" } function echoHeading () { echo -e `eval ${commandCurrentDate}`"${formatInvertColors}${1}${formatReset}" } #give parameter in one string #correct: runCommand "echo hello world" #wrong: runCommand echo hello world function runCommand () { if [ "${2}" != "" ] then echo -e `eval ${commandCurrentDate}`${formatUnderlined}${2}${formatReset} fi echo -e `eval ${commandCurrentDate}`"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 exitCode="80" echoError "Command returned ${result0} ${result1}. Exiting with $exitCode." exit $exitCode fi } #usage command, variable_name [, information] #e.g. runCommandReturnOutputExitOnError ls lsoutvariablename 'get ls output' function runCommandReturnOutputExitOnError () { if [ "${1}" == "" ] then echoError "${FUNCNAME[*]}: 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 83." exit 83 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 84, since exit requested on success." exit 84 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 85 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 86 fi if [ "${2}" != "" ] then echoSuccess "OK: ${2} found: ${1}" fi } function createDirectoryIfNotExists () { if [ -d "${1}" ] then echoNormal "Info: 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 87 #createDirectoryIfNotExists fi fi } function checkProcessRunning () { if [ "$(ps ax | grep -v grep | grep ${1})" == "" ] then echo "false" else echo "true" fi } #usage: # ask "continue?" "y" "n" # if [ "$?" == "1" ] # then # echo "first awnswer" # fi # return: echoes 1 for first answer 2 for second .. function ask () { #echo parameters for debugging #for (( i=1; i<=$#; i++ )) #do # eval arg=\${$i} # echoNormal "\$$i=$arg" #done if [ "${2}" == "" ]; then echoError "At leas 2 parameter needed. A question and 1 choice."; exit 88; fi #if [ "${4}" != "" ]; then echoError "less than 4 parameters needed"; exit 88; fi echo -en `eval ${commandCurrentDate}`"${formatInvertColors}${1}${colorForegroundGreen}(" echo -en "" #print answer possibilities for (( i=2; i<=$#; i++ )) do eval arg=\${$i} if [ "${i}" != "2" ]; then echo -en "/"; fi echo -en "${arg}" done echo -en ")${formatReset} " #key input loop while [ 1 ] do old_stty_cfg=$(stty -g) stty raw -echo answer=$(head -c 1) stty $old_stty_cfg iKey=`printf "%d" "\"${answer}"` #echo " ${answer} (${iKey})" #evaluate pressed key agains all possible keys #check special case STRG + C if [ "${iKey}" == "3" ] then echo " ${answer} (${iKey})" echo -e "${colorError}got ^C, exiting with 999${formatReset}" exit 999 #^C fi #check agains all possible answers for (( i=2; i<=$#; i++ )) do eval arg=\${$i} cFirst=`printf "%c" "${arg}"` if [ "${answer}" == "${cFirst}" ] then echoNormal " ${answer} (${iKey})" let "answer=i-1" #echoError "DEBUG: answer=${answer}" return "${answer}" fi done done } function askCommandsRun () { if [ "${1}" == "" ]; then echoError "at leas 1 parameter needed"; exit 89; fi if [ "${5}" != "" ]; then echoError "less than 5 parameters needed"; exit 89; fi sTextQuestion="${1}" sCMD1="${2}" sCMD1="${3}" sCMD1="${4}" sCMDList="${2}" if [ "${3}" != "" ]; then sCMDList="${sCMDList}\n${3}"; fi if [ "${4}" != "" ]; then sCMDList="${sCMDList}\n${4}"; fi ask "${sTextQuestion}\nRun following commands..\n${sCMDList}\n" "y" "n" if [ "$?" == "1" ] then if [ "${2}" != "" ]; then runCommandExitOnError "${2}"; fi if [ "${3}" != "" ]; then runCommandExitOnError "${3}"; fi if [ "${4}" != "" ]; then runCommandExitOnError "${4}"; fi fi } function inFileReplaceLines () { if [ "${1}" == "" ]; then echoError "4 parameters needed"; exit 90; fi if [ "${5}" != "" ]; then echoError "less than 5 parameters needed"; exit 90; fi fileIn="${1}" fileOut="${2}" sLineFrom="${3}" sLineTo="${4}" checkFileAndExitIfNotExists "${fileIn}" ## iLineFromIn=`grep -ce "${sLineFrom}" "${fileIn}"` if [ "${iLineFromIn}" != "1" ]; then echoError "not 1 sLineFromIn match in fileIn"; exit 89; fi iLineFromIn=`grep -noP "${sLineFrom}" "${fileIn}" | grep -oP "\d*"` #echo $iLineFromIn ## iLineToIn=`grep -ce "${sLineTo}" "${fileIn}"` if [ "${iLineToIn}" != "1" ]; then echoError "not 1 sLineFromTo match in fileIn"; exit 89; fi iLineToIn=`grep -noP "${sLineTo}" "${fileIn}" | grep -oP "\d*"` #echo $iLineToIn ## #sed -n ${sedlines}p ${fin} >> ${fout} sTextCopy=`head -n ${iLineToIn} ${fileIn} | tail -n +${iLineFromIn}` #sTextCopy=`cat ${fileIn} | head -n ${iLineToIn} | tail -n +${iLineFromIn}` #echo "${sTextCopy}" ## iLineFromOut=`grep -ce "${sLineFrom}" "${fileOut}"` iLineToOut=`grep -ce "${sLineTo}" "${fileOut}"` if [ "${iLineFromOut}" == "1" ] && [ "${iLineToOut}" == "1" ] then iLineFromOut=`grep -noP "${sLineFrom}" "${fileOut}" | grep -oP "\d*"` iLineToOut=`grep -noP "${sLineTo}" "${fileOut}" | grep -oP "\d*"` #echo "removing lines from ${iLineFromOut} to ${iLineToOut} in ${fileOut}" sed -i_sed_backup ${iLineFromOut},${iLineToOut}d ${fileOut} #sTextFileOut=`head -n ${iLineFromOut} ${fileOut}` fi #echo "appending lines to ${fileOut}" #append echo "${sTextCopy}" >> ${fileOut} } echoVarValue () { sVarName="$1" local sVarValue="${!sVarName}" #echo -e "${colorForegroundGreen}${sVarName}${formatReset}=${formatBold}${sVarValue}${formatReset}" echo -e "${sVarName}${colorForegroundYellow}=${formatReset}${formatBold}${sVarValue}${formatReset}" } function unit_test () { echo "not implemented" #testFunction="runCommandExitOnError" #echoHeading "testing ${testFunction}" #bash -c "${testFunction} ''" #bash -c "${testFunction} 'echo "test success"" ##${testFunction} 'failtest' #echo #testFunction="ask" #${testFunction} '' #${testFunction} 'question' #${testFunction} 'question y' #${testFunction} 'question y n' #${testFunction} 'question y n 4th' }