From 868ff8831fd5b4bf0f7e75673178fbf51da98a91 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Mon, 8 May 2023 16:45:51 -0700 Subject: [PATCH] added catch fo rmissing logfile --- functions | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/functions b/functions index c0ed681..c15a656 100644 --- a/functions +++ b/functions @@ -2,7 +2,7 @@ # pyr0ball script functions library # Initial Vars -functionsrev=1.1.12 +functionsrev=1.1.13 #scriptname="${BASH_SOURCE[0]##*/}" #rundir="${BASH_SOURCE[0]%/*}" #rundir_absolute=$(cd $rundir && pwd) @@ -411,19 +411,6 @@ popdfail(){ fail "$@" } -logger(){ - $@ 2>&1 | tee >( - while IFS= read -r line; do - # strip any escaped strings for logging output - _line=$(echo $line | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | tr -dc '[:print:]') - # only print out non-empty lines to log - if [[ "$_line" != "" ]] ; then - printf '[%s] %s\n' "${scriptname}][${pretty_date}" "$_line" - fi - done >> $logfile - ) -} - run(){ _cmd=$@ if [[ $dry_run != true ]] ; then @@ -442,6 +429,23 @@ run-and-log(){ fi } +logger(){ + if [ ! -f $logfile ] ; then + run mkdir -p ${rundir%/*} + touch $logfile + fi + $@ 2>&1 | tee >( + while IFS= read -r line; do + # strip any escaped strings for logging output + _line=$(echo $line | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | tr -dc '[:print:]') + # only print out non-empty lines to log + if [[ "$_line" != "" ]] ; then + printf '[%s] %s\n' "${scriptname}][${pretty_date}" "$_line" + fi + done >> $logfile + ) +} + ctrl_c(){ echo -e "\n" fail "User interrupted with Ctrl-C"