feat(unitproc):Add function writeArg for writing Bash vars to disk
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Thu, 2 Jul 2020 00:16:47 +0000 (00:16 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Thu, 2 Jul 2020 00:16:47 +0000 (00:16 +0000)
unitproc/bktemp-writeArg [new file with mode: 0644]

diff --git a/unitproc/bktemp-writeArg b/unitproc/bktemp-writeArg
new file mode 100644 (file)
index 0000000..56936ea
--- /dev/null
@@ -0,0 +1,45 @@
+#!/bin/bash
+
+# Desc: Template function to write data supplied as argument
+
+yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
+die() { yell "$*"; exit 111; }
+try() { "$@" || die "cannot $*"; }
+writeArg(){
+    # Desc: Writes first argument with subsequent arguments as options
+    # Usage: writeArg "$(echo "Data to be written.")" [output path] ([cmd1] [cmd2] [cmd3] [cmd4]...)
+    # Input: arg1: data to be written
+    #        arg2: output path
+    #        arg3+: command strings (ex: "gpsbabel -i nmea -f - -o kml -F - ")
+    # Output: file written to disk
+    # Example: decrypt multiple large files in parallel
+    #          writeArg "$(cat /tmp/largefile1.gpg)" /tmp/largefile1 "gpg --decrypt" &
+    #          writeArg "$(cat /tmp/largefile2.gpg)" /tmp/largefile2 "gpg --decrypt" &
+    #          writeArg "$(cat /tmp/largefile3.gpg)" /tmp/largefile3 "gpg --decrypt" &
+    # Depends: bash 5
+    
+    # Set command strings
+    if ! [ -z "$3" ]; then CMD1="$3"; else CMD1="tee /dev/null "; fi # command string 1
+    if ! [ -z "$4" ]; then CMD2="$4"; else CMD2="tee /dev/null "; fi # command string 2
+    if ! [ -z "$5" ]; then CMD3="$5"; else CMD3="tee /dev/null "; fi # command string 3
+    if ! [ -z "$6" ]; then CMD4="$6"; else CMD4="tee /dev/null "; fi # command string 4
+
+    # Debug
+    yell "CMD1:$CMD1"
+    yell "CMD2:$CMD2"
+    yell "CMD3:$CMD3"
+    yell "CMD4:$CMD4"
+    # Write
+    echo "$1" | $CMD1 | $CMD2 | $CMD3 | $CMD4 > "$2";
+
+} # Get duration (ex: PT10M4S )
+
+#==BEGIN sample code==
+myFile="/tmp/$(date +%s)..original"
+echo "how are you doing?" > "$myFile"
+myVAR="$(cat $myFile)"
+writeArg "$myVAR" "$myFile..LOUD_CHILD" "tee /tmp/cloneityclone " "tr '[:lower:]' '[:upper:]'"
+#==END sample code==
+
+# Author: Steven Baltakatei Sandoval
+# License: GPLv3+