X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02.git/blobdiff_plain/b562e6ab07a5f2236b922d0c68f03ac7ff006c5a..cfd105b555e4f3b898c79a5d58e16c803036cb06:/exec/bklog?ds=inline diff --git a/exec/bklog b/exec/bklog index d6de8cb..6fc61e4 100644 --- a/exec/bklog +++ b/exec/bklog @@ -11,7 +11,7 @@ dirTmpDefault="/dev/shm"; # Default parent of working directory # Script Metadata scriptName="bklog"; # Define basename of script file. -scriptVersion="0.1.7"; # Define version of script. +scriptVersion="0.1.8"; # Define version of script. scriptURL="https://gitlab.com/baltakatei/ninfacyzga-01"; # Define wesite hosting this script. scriptTimeStart="$(date +%Y%m%dT%H%M%S.%N)"; # YYYYmmddTHHMMSS.NNNNNNNNN scriptHostname=$(hostname); # Save hostname of system running this script. @@ -284,6 +284,46 @@ checkAgePubkey() { return 1; fi; } # Check age pubkey +checkMakeTar() { + # Desc: Checks that a valid tar archive exists, creates one otherwise + # Usage: checkMakeTar [ path ] + # Version: 1.0.2 + # Input: arg1: path of tar archive + # Output: exit code 0 : tar readable + # exit code 1 : tar missing; created + # exit code 2 : tar not readable; moved; replaced + # Depends: bash 5, date 8, tar 1, try() + local pathTar returnFlag0 returnFlag1 returnFlag2 + pathTar="$1"; + + # Check if file is a valid tar archive + if tar --list --file="$pathTar" 1>/dev/null 2>&1; then + ## T1: return success + returnFlag0="tar valid"; + else + ## F1: Check if file exists + if [[ -f "$pathTar" ]]; then + ### T: Rename file + try mv "$pathTar" "$pathTar""--broken--""$(date +%Y%m%dT%H%M%S)" && \ + returnFlag1="tar moved"; + else + ### F: - + : + fi; + ## F2: Create tar archive, return 0 + try tar --create --file="$pathTar" --files-from=/dev/null && \ + returnFlag2="tar created"; + fi; + + # Determine function return code + if [[ "$returnFlag0" = "tar valid" ]]; then + return 0; + elif [[ "$returnFlag2" = "tar created" ]] && ! [[ "$returnFlag1" = "tar moved" ]]; then + return 1; # tar missing so created + elif [[ "$returnFlag2" = "tar created" ]] && [[ "$returnFlag1" = "tar moved" ]]; then + return 2; # tar not readable so moved; replaced + fi; +} # checks if arg1 is tar; creates one otherwise dateShort(){ # Desc: Date without separators (YYYYmmdd) # Usage: dateShort ([str date]) @@ -624,6 +664,73 @@ timeDuration(){ fi } # Get duration (ex: PT10M4S ) +timeUntilNextDay(){ + # Desc: Report seconds until next day. + # Version: 1.0.2 + # Output: stdout: integer seconds until next day + # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0 + # Usage: timeUntilNextDay + # Usage: if ! myTTL="$(timeUntilNextDay)"; then yell "ERROR in if statement"; exit 1; fi + # Depends: date 8, echo 8, yell, try + + local returnState timeCurrent timeNextDay secondsUntilNextDay returnState + timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second. + timeNextDay="$(date -d "$timeCurrent next day" --iso-8601=date)"; # Produce timestamp of beginning of tomorrow with resolution of 1 second. + secondsUntilNextDay="$(( $(date +%s -d "$timeNextDay") - $(date +%s -d "$timeCurrent") ))" ; # Calculate seconds until closest future midnight (res. 1 second). + if [[ "$secondsUntilNextDay" -gt 0 ]]; then + returnState="true"; + elif [[ "$secondsUntilNextDay" -eq 0 ]]; then + returnState="warning_zero"; + yell "WARNING:Reported time until next day exactly zero."; + elif [[ "$secondsUntilNextDay" -lt 0 ]]; then + returnState="warning_negative"; + yell "WARNING:Reported time until next day is negative."; + fi + + try echo "$secondsUntilNextDay"; # Report + + # Determine function return code + if [[ "$returnState" = "true" ]]; then + return 0; + elif [[ "$returnState" = "warning_zero" ]]; then + return 1; + elif [[ "$returnState" = "warning_negative" ]]; then + return 2; + fi +} # Report seconds until next day +timeUntilNextHour(){ + # Desc: Report seconds until next hour + # Version 1.0.1 + # Output: stdout: integer seconds until next hour + # Output: exit code 0 if stdout > 0; 1 if stdout = 0; 2 if stdout < 0 + # Usage: timeUntilNextHour + # Usage: if ! myTTL="$(timeUntilNextHour)"; then yell "ERROR in if statement"; exit 1; fi + + local returnState timeCurrent timeNextHour secondsUntilNextHour + timeCurrent="$(date --iso-8601=seconds)"; # Produce `date`-parsable current timestamp with resolution of 1 second. + timeNextHour="$(date -d "$timeCurrent next hour" --iso-8601=hours)"; # Produce `date`-parsable current time stamp with resolution of 1 second. + secondsUntilNextHour="$(( $(date +%s -d "$timeNextHour") - $(date +%s -d "$timeCurrent") ))"; # Calculate seconds until next hour (res. 1 second). + if [[ "$secondsUntilNextHour" -gt 0 ]]; then + returnState="true"; + elif [[ "$secondsUntilNextHour" -eq 0 ]]; then + returnState="warning_zero"; + yell "WARNING:Reported time until next hour exactly zero."; + elif [[ "$secondsUntilNextHour" -lt 0 ]]; then + returnState="warning_negative"; + yell "WARNING:Reported time until next hour is negative."; + fi; + + try echo "$secondsUntilNextHour"; # Report + + # Determine function return code + if [[ "$returnState" = "true" ]]; then + return 0; + elif [[ "$returnState" = "warning_zero" ]]; then + return 1; + elif [[ "$returnState" = "warning_negative" ]]; then + return 2; + fi; +} # Report seconds until next hour validateInput() { # Desc: Validates Input # Usage: validateInput [str input] [str input type] @@ -859,8 +966,8 @@ magicParseProcessStrings() { if [[ -z "$element" ]]; then yell "ERROR:Empty output file extension specified. Exiting."; exit 1; fi; done ## Make sure that no process string starts with '-' (ex: if only one arg supplied after '-p' option) for element in "${argProcStrings[@]}"; do - if [[ ! "$element" =~ ^[-][[:print:]]*$ ]] && [[ "$element" =~ ^[[:print:]]*$ ]]; then - yell "ERROR:Illegal character '-' at start of process string element. Option syntax error?"; + if [[ "$element" =~ ^[-][[:print:]]*$ ]] && [[ ! "$element" =~ ^[[:print:]]*$ ]]; then + yell "ERROR:Illegal character '-' at start of process string element:\"$element\""; exit 1; fi; done; vbm "STATUS:Quick check shows argProcStrings and argProcFileExts appear to have valid contents."; procStrings=("${argProcStrings[@]}"); # Export process command strings @@ -1108,8 +1215,6 @@ main() { magicParseProcessStrings; # Sets arrays: procStrings, procFileExts ## React to "-l" (output file label) option magicParseLabel; # sets label (ex: "_location") - ## React to "-w" (how to name raw stdin file) option - magicParseStoreRaw; # sets raw_suffix # Perform secondary setup operations ## Set script lifespan (scriptTTL from scriptTTL_TE) @@ -1135,9 +1240,9 @@ main() { while read -r -t "$bufferTTL" line && [[ $SECONDS -lt "$bufferTOD" ]]; do # Append line to buffer array buffer+=("$line"); - echo "DEBUG:Processing line:$lineCount"; - echo "DEBUG:Current line :$line"; - echo "DEBUG:buf elem count :${#buffer[@]}"; + vbm "DEBUG:Processing line:$lineCount"; + vbm "DEBUG:Current line :$line"; + vbm "DEBUG:buf elem count :${#buffer[@]}"; ((lineCount++)); done; # Create dir_tmp if missing