#!/bin/bash (source) Import Terminal logLevelError=10 logLevelTerse=20 logLevelNormal=30 logLevelVerbose=40 logLevelDebug=50 [ "$logMode" ] || logMode="Normal" export verboseFD=3 export normalFD=4 export terseFD=5 export errorFD=6 export debugFD=7 export logFD=8 export questionFD=9 [ -z "${logfile}" ] && logfile="/dev/null" if [ -n "$(echo -e)" ] then echo=echo else echo="echo -e" fi Is_Log_Mode() { [ "$logMode" = "$1" ] } Log_Function() { local message local echoflags Parameters "$@" message color FD echoflags $echo $echoflags "${colorGray}${scriptName}:${colorNormal} ${color}$message${colorNormal}" >&$FD $echo $echoflags "$scriptName: $message" >&$logFD } Log_Error() { Log_Function "$*" "${colorBoldRed}" "${errorFD}" } Log_Normal() { Log_Function "$*" "${colorCyan}" "${normalFD}" } Log_Terse() { Log_Function "$*" "${colorBoldCyan}" "${terseFD}" } Log_Verbose() { Log_Function "$*" "${colorNormal}" "${verboseFD}" } Log_Debug() { Log_Function "$*" "${colorRedWhite}" "${debugFD}" } Quiet() { "$@" &> /dev/null ;} Verbose() { "$@" 1>&$verboseFD 2>&$verboseFD ;} # Ask a question. If the answer is "n", quit. Ask_Continue() { if [ "$logLevel" -lt "$logLevelNormal" ] then return 0 fi if [ "$1" ] then Log_Question "$1" fi Log_Question "Press Enter to continue or Ctrl-C to cancel." read if [ "$REPLY" = "n" ] then exit 1 fi if [ "$?" != "0" ] then exit $? fi } Log_Question() { # Manager expects ${colorNormal}${colorNormal} in questions Log_Function "$*" "${colorNormal}${colorNormal}${colorBoldCyan}" "${questionFD}" } Ask_Option() { local question=$1 local defaultanswer=$2 local lambda=$3 Log_Question "${question}" # detsch, maybe this loop should be continued until some valid # response is received while true do read #if [ "$REPLY" != "" -o "${questionStream}" = "" ] if [ -z "$REPLY" -a -n "${defaultanswer}" ] then Log_Verbose "Using default answer (${defaultanswer})" REPLY="${defaultanswer}" fi if [ "$REPLY" != "" ] && [ -n "$lambda" ] then if eval "${lambda}" then break fi elif [ -n "$REPLY" ] then break fi done if [ "$?" != "0" ] then exit $? fi } Ask() { local question Parameters "$@" question REPLY=foo Ask_Option "$question [Y/n]" "yes" '[ -n "$REPLY" -a -z "$(echo $REPLY | sed -r -e "s/((Y|y)(ES|es)?|(N|n)(O|o)?)//")" ]' if expr "$REPLY" : "N" >/dev/null || expr "$REPLY" : "n" >/dev/null then return 1 else return 0 fi } Progress_Start() { local message Parameters "$@" message Log_Function "$message *" "${colorCyan}" "${normalFD}" "-n" spinControl=0 } Progress_Move() { $echo -n "${colorCyan}" case "$spinControl" in 0) echo -n "|" >&${normalFD} ; spinControl=1 ;; 1) echo -n "/" >&${normalFD} ; spinControl=2 ;; 2) echo -n "-" >&${normalFD} ; spinControl=3 ;; 3) echo -n "\\" >&${normalFD} ; spinControl=0 ;; esac # esac is ridiculous. $echo -n ${colorNormal} } Progress_End() { $echo "${colorNormal}" 1>&2 } # Prints an error message and exits Die() { [ "" = "$*" ] || Log_Error "$@" exit 1 } # Error constants export errorNotFound=2 # Prints an error message and exits with a given code Die_With() { status=$1 shift [ "" = "$*" ] || Log_Error "$@" exit $status } # Prints a message and exits successfully Pass_Away() { [ "" = "$*" ] || Log_Error "$@" exit 0 } Exit_With() { ret=$1 shift if [ 0 -eq $ret ] then [ -n "$*" ] && Log_Terse "$@" else [ -n "$*" ] && Log_Error "$@" fi exit $ret }