| 1 | #!/bin/bash |
| 2 | |
| 3 | # Desc: Template function to write data supplied as argument |
| 4 | |
| 5 | yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370 |
| 6 | die() { yell "$*"; exit 111; } |
| 7 | try() { "$@" || die "cannot $*"; } |
| 8 | writeArg(){ |
| 9 | # Desc: Writes first argument with subsequent arguments as options |
| 10 | # Usage: writeArg "$(echo "Data to be written.")" [output path] ([cmd1] [cmd2] [cmd3] [cmd4]...) |
| 11 | # Input: arg1: data to be written |
| 12 | # arg2: output path |
| 13 | # arg3+: command strings (ex: "gpsbabel -i nmea -f - -o kml -F - ") |
| 14 | # Output: file written to disk |
| 15 | # Example: decrypt multiple large files in parallel |
| 16 | # writeArg "$(cat /tmp/largefile1.gpg)" /tmp/largefile1 "gpg --decrypt" & |
| 17 | # writeArg "$(cat /tmp/largefile2.gpg)" /tmp/largefile2 "gpg --decrypt" & |
| 18 | # writeArg "$(cat /tmp/largefile3.gpg)" /tmp/largefile3 "gpg --decrypt" & |
| 19 | # Depends: bash 5 |
| 20 | |
| 21 | # Set command strings |
| 22 | if ! [ -z "$3" ]; then CMD1="$3"; else CMD1="tee /dev/null "; fi # command string 1 |
| 23 | if ! [ -z "$4" ]; then CMD2="$4"; else CMD2="tee /dev/null "; fi # command string 2 |
| 24 | if ! [ -z "$5" ]; then CMD3="$5"; else CMD3="tee /dev/null "; fi # command string 3 |
| 25 | if ! [ -z "$6" ]; then CMD4="$6"; else CMD4="tee /dev/null "; fi # command string 4 |
| 26 | |
| 27 | # Debug |
| 28 | yell "CMD1:$CMD1" |
| 29 | yell "CMD2:$CMD2" |
| 30 | yell "CMD3:$CMD3" |
| 31 | yell "CMD4:$CMD4" |
| 32 | # Write |
| 33 | echo "$1" | $CMD1 | $CMD2 | $CMD3 | $CMD4 > "$2"; |
| 34 | |
| 35 | } # Get duration (ex: PT10M4S ) |
| 36 | |
| 37 | #==BEGIN sample code== |
| 38 | myFile="/tmp/$(date +%s)..original" |
| 39 | echo "how are you doing?" > "$myFile" |
| 40 | myVAR="$(cat $myFile)" |
| 41 | writeArg "$myVAR" "$myFile..LOUD_CHILD" "tee /tmp/cloneityclone " "tr '[:lower:]' '[:upper:]'" |
| 42 | #==END sample code== |
| 43 | |
| 44 | # Author: Steven Baltakatei Sandoval |
| 45 | # License: GPLv3+ |