@@ -380,14 +380,17 @@ PrefixFileLines() {
380380}
381381
382382RunAndLog () {
383- # RunAndLog [RunningNoticeType] [OutputNoticeType] [ErrorNoticeType] [ErrorMessage] [Command]
383+ # RunAndLog [RunningNoticeType] [Prefix:[OutputNoticeType]] [ErrorNoticeType] [ErrorMessage] [Command]
384+ # To skip an optional argument, pass an empty string
384385 local -l RunningNoticeType=${1-}
385386 local -l OutputNoticeType=${2-}
386387 local -l ErrorNoticeType=${3-}
387388 local ErrorMessage=${4-}
388389 shift 4
389390 local -a Command=(" ${@ } " )
390391
392+ NoticeTypes_Regex=' info|notice|warn|error|debug|trace'
393+
391394 local Prefix=' '
392395 if [[ ${OutputNoticeType} == * :* ]]; then
393396 Prefix=" ${OutputNoticeType%%:* } :"
@@ -407,24 +410,25 @@ RunAndLog() {
407410
408411 local ErrToNull=false
409412 local OutToNull=false
410- if [[ ${OutputNoticeType-} =~ info| notice| warn| error| debug| trace ]]; then
411- OutputFile=$( mktemp -t " ${APPLICATION_NAME} .${FUNCNAME[0]} .RunAndLogOutputFile.XXXXXXXXXX" )
412- fi
413- if [[ ${OutputNoticeType-} =~ errtonull ]]; then
413+ if [[ ${OutputNoticeType-} =~ errtonull| bothtonull ]]; then
414414 ErrToNull=true
415415 fi
416- if [[ ${OutputNoticeType-} =~ outtonull ]]; then
416+ if [[ ${OutputNoticeType-} =~ outtonull| bothtonull ]]; then
417417 OutToNull=true
418418 fi
419+ if [[ ${ErrToNull} != true || ${OutToNull} != true ]] && [[ ${OutputNoticeType-} =~ ${NoticeTypes_Regex} ]]; then
420+ # If the output notice type is set, save the output to a file
421+ OutputFile=$( mktemp -t " ${APPLICATION_NAME} .${FUNCNAME[0]} .RunAndLogOutputFile.XXXXXXXXXX" )
422+ fi
419423 local -i result=0
420424 if [[ ${ErrToNull} == true && ${OutToNull} == true ]]; then
421425 # Both stdout and stderr are redirected to /dev/null
422426 " ${Command[@]} " & > /dev/null || result=$?
423- elif [[ -n ${OutputFile-} && ${ErrToNull} == true ]]; then
424- # stderr redircted to null, stdout redirected to output file
427+ elif [[ ${ErrToNull} == true && -n ${OutputFile-} ]]; then
428+ # stderr redircted to /dev/ null, stdout redirected to output file
425429 " ${Command[@]} " > " ${OutputFile} " 2> /dev/null || result=$?
426- elif [[ -n ${OutputFile-} && ${OutToNull} == true ]]; then
427- # stdout redircted to null, stderr redirected to output file
430+ elif [[ ${OutToNull} == true && -n ${OutputFile-} ]]; then
431+ # stdout redircted to /dev/ null, stderr redirected to output file
428432 " ${Command[@]} " 2> " ${OutputFile} " > /dev/null || result=$?
429433 elif [[ -n ${OutputFile-} ]]; then
430434 # Both stdout and stderr redirected to output file
@@ -442,8 +446,11 @@ RunAndLog() {
442446
443447 [[ ${result} -eq 0 ]] && return
444448
445- ${ErrorNoticeType} \
446- " ${ErrorMessage} " \
447- " Failing command: ${C["FailingCommand"]}${CommandText} "
449+ if [[ -n ${ErrorNoticeType-} ]]; then
450+ # If the error notice type is set, log the error
451+ ${ErrorNoticeType} \
452+ " ${ErrorMessage} " \
453+ " Failing command: ${C["FailingCommand"]}${CommandText} "
454+ fi
448455 return ${result}
449456}
0 commit comments