feat(unitproc/get_rand_line):Bash script that gets random line
[BK-2020-03.git] / unitproc / bkFreqWrite
1 #!/bin/bash
2 # Desc: Writes stdin to disk every 5 seconds
3 yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique
4 die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370
5 try() { "$@" || die "cannot $*"; } #o
6 declare -a buffer # Initialize buffer array
7 scriptTTL="15";
8 bufferTTL="5";
9 magicWriteBuffer() {
10 printf "%s\n" "${buffer[@]}" | cat | cat | cat >> /dev/shm/$(date +%s)..bkFreqWrite-output1.txt;
11 printf "%s\n" "${buffer[@]}" | cat | cat | cat >> /dev/shm/$(date +%s)..bkFreqWrite-output2.txt;
12 printf "%s\n" "${buffer[@]}" | cat | cat | cat >> /dev/shm/$(date +%s)..bkFreqWrite-output3.txt;
13 }
14
15 bufferRound=0;
16 # Run until script TTL seconds pass
17 while [[ $SECONDS -lt "scriptTTL" ]]; do
18 bufferTOD="$((SECONDS + $bufferTTL))";
19 lineCount=0;
20 # Consume stdin to fill buffer until buffer time-of-death (TOD) arrives
21 while read -r -t "$bufferTTL" line && [[ $SECONDS -lt "$bufferTOD" ]]; do
22 # Append line to buffer array
23 buffer+=("$line");
24 echo "DEBUG:Processing line:$lineCount";
25 echo "DEBUG:Current line :$line";
26 echo "DEBUG:buf elem count :${#buffer[@]}";
27 ((lineCount++));
28 done;
29 # Export buffer to asynchronous processing.
30 magicWriteBuffer &
31 unset buffer; # Clear buffer array for next bufferRound
32 # Increment buffer round
33 ((bufferRound++));
34 done;
35
36 # Author: Steven Baltakatei Sandoval
37 # License: GPLv3+