| 1 | #!/bin/bash |
| 2 | # Desc: Compresses, encrypts, and writes stdin every 5 seconds |
| 3 | |
| 4 | #==BEGIN Define script parameters== |
| 5 | #===BEGIN Initialize variables=== |
| 6 | |
| 7 | # Logging Behavior parameters |
| 8 | scriptTTL_TE="day"; # Time element at the end of which script terminates |
| 9 | scriptTTL="15"; # (temp) Time (seconds) until script terminates |
| 10 | bufferTTL="5"; # Time-to-live (seconds) for each buffer round |
| 11 | dirTempDefault="/dev/shm"; # Default parent of working directory |
| 12 | |
| 13 | # Script Metadata |
| 14 | scriptVersion="0.1.1"; # Define version of script. |
| 15 | scriptName="bklog"; # Define basename of script file. |
| 16 | PATH="$HOME/.local/bin:$PATH"; # Add "$(systemd-path user-binaries)" path in case user apps saved there |
| 17 | |
| 18 | # Arrays |
| 19 | declare -a buffer # Initialize buffer array |
| 20 | |
| 21 | # Variables |
| 22 | |
| 23 | #===END Initialize variables=== |
| 24 | |
| 25 | #===BEGIN Declare local script functions=== |
| 26 | yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique |
| 27 | die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370 |
| 28 | try() { "$@" || die "cannot $*"; } #o |
| 29 | magicProcessWriteBuffer() { |
| 30 | printf "%s\\n" "${buffer[@]}" | cat | cat | cat >> "/dev/shm/$(date +%s)..bkFreqWrite-output1.txt"; |
| 31 | printf "%s\\n" "${buffer[@]}" | cat | cat | cat >> "/dev/shm/$(date +%s)..bkFreqWrite-output2.txt"; |
| 32 | printf "%s\\n" "${buffer[@]}" | cat | cat | cat >> "/dev/shm/$(date +%s)..bkFreqWrite-output3.txt"; |
| 33 | } # Process and Write buffer |
| 34 | main() { |
| 35 | bufferRound=0; |
| 36 | # Run until script TTL seconds pass |
| 37 | while [[ $SECONDS -lt "scriptTTL" ]]; do |
| 38 | bufferTOD="$((SECONDS + bufferTTL))"; |
| 39 | lineCount=0; |
| 40 | # Consume stdin to fill buffer until buffer time-of-death (TOD) arrives |
| 41 | while read -r -t "$bufferTTL" line && [[ $SECONDS -lt "$bufferTOD" ]]; do |
| 42 | # Append line to buffer array |
| 43 | buffer+=("$line"); |
| 44 | echo "DEBUG:Processing line:$lineCount"; |
| 45 | echo "DEBUG:Current line :$line"; |
| 46 | echo "DEBUG:buf elem count :${#buffer[@]}"; |
| 47 | ((lineCount++)); |
| 48 | done; |
| 49 | # Export buffer to asynchronous processing. |
| 50 | magicProcessWriteBuffer & |
| 51 | unset buffer; # Clear buffer array for next bufferRound |
| 52 | # Increment buffer round |
| 53 | ((bufferRound++)); |
| 54 | done; |
| 55 | } |
| 56 | |
| 57 | #===END Declare local script functions=== |
| 58 | #==END Define script parameters== |
| 59 | |
| 60 | #==BEGIN Perform work and exit== |
| 61 | main "$@" # Run main function. |
| 62 | exit 0; |
| 63 | #==END Perform work and exit== |
| 64 | |
| 65 | # Author: Steven Baltakatei Sandoval; |
| 66 | # License: GPLv3+ |