fix(unitproc):timeDuration: Add missing $ to var, add ver\#
[BK-2020-03.git] / unitproc / bktemp-appendArgTar
CommitLineData
0f8ac166
SBS
1#!/bin/bash
2
3# Desc: Template function to write data supplied as argument
4
5yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
6die() { yell "$*"; exit 111; }
7try() { "$@" || die "cannot $*"; }
8appendArgTar(){
9 # Desc: Writes first argument to temporary file with arguments as options, then appends file to tar
10 # Usage: writeArg "$(echo "Data to be written.")" [name of file to be inserted] [tar path] [temp dir] ([cmd1] [cmd2] [cmd3] [cmd4]...)
11 # Input: arg1: data to be written
12 # arg2: file name of file to be inserted into tar
13 # arg3: tar archive path (must exist first)
14 # arg4: temporary working dir
15 # arg5+: command strings (ex: "gpsbabel -i nmea -f - -o kml -F - ")
16 # Output: file written to disk
17 # Example: decrypt multiple large files in parallel
18 # appendArgTar "$(cat /tmp/largefile1.gpg)" "largefile1" $HOME/archive.tar /tmp "gpg --decrypt" &
19 # appendArgTar "$(cat /tmp/largefile2.gpg)" "largefile2" $HOME/archive.tar /tmp "gpg --decrypt" &
20 # appendArgTar "$(cat /tmp/largefile3.gpg)" "largefile3" $HOME/archive.tar /tmp "gpg --decrypt" &
21
22 # Depends: bash 5
23
24 # Set file name
25 if ! [ -z "$2" ]; then FILENAME="$2"; else yell "ERROR:Not enough arguments."; exit 1; fi
26
27 # Check tar path is a file
28 if [ -f "$3" ]; then TAR_PATH="$3"; else yell "ERROR:Tar archive arg not a file."; exit 1; fi
29
30 # Check temp dir arg
31 if ! [ -z "$4" ]; then TMP_DIR="$3"; else yell "ERROR:No temporary working dir set."; exit 1; fi
32
33 # Set command strings
34 if ! [ -z "$5" ]; then CMD1="$5"; else CMD1="tee /dev/null "; fi # command string 1
35 if ! [ -z "$6" ]; then CMD2="$6"; else CMD2="tee /dev/null "; fi # command string 2
36 if ! [ -z "$7" ]; then CMD3="$7"; else CMD3="tee /dev/null "; fi # command string 3
37 if ! [ -z "$8" ]; then CMD4="$8"; else CMD4="tee /dev/null "; fi # command string 4
38
39 # Debug
40 yell "CMD1:$CMD1"
41 yell "CMD2:$CMD2"
42 yell "CMD3:$CMD3"
43 yell "CMD4:$CMD4"
44 # Write to temporary working dir
45 echo "$1" | $CMD1 | $CMD2 | $CMD3 | $CMD4 > "$TMP_DIR"/"$FILENAME";
46
47 # Append to tar
48 try tar --append --directory="$TMP_DIR" --file="$TAR_PATH" "$FILENAME";
49
50} # Append Bash var to file appended to Tar archive
51
52#==BEGIN sample code==
53myFile="/tmp/$(date +%s)..original"
54echo "how are you doing?" > "$myFile"
55myVAR="$(cat $myFile)"
56writeArg "$myVAR" "$myFile..LOUD_CHILD" "tee /tmp/cloneityclone " "tr '[:lower:]' '[:upper:]'"
57#==END sample code==
58
59# Author: Steven Baltakatei Sandoval
60# License: GPLv3+