5d42dc20e37e2f5ce45e731928c3f1c887131401
3 # Desc: Template function to write data supplied as argument
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 $*"; }
9 # Desc: Writes first argument to temporary file with arguments as options, then appends file to tar
10 # Usage: appendArgTar "$(echo "Data to be written.")" [name of file to be inserted] [tar path] [temp dir] ([cmd1] [cmd2] [cmd3] [cmd4]...)
12 # Input: arg1: data to be written
13 # arg2: file name of file to be inserted into tar
14 # arg3: tar archive path (must exist first)
15 # arg4: temporary working dir
16 # arg5+: command strings (ex: "gpsbabel -i nmea -f - -o kml -F - ")
17 # Output: file written to disk
18 # Example: decrypt multiple large files in parallel
19 # appendArgTar "$(cat /tmp/largefile1.gpg)" "largefile1" $HOME/archive.tar /tmp "gpg --decrypt" &
20 # appendArgTar "$(cat /tmp/largefile2.gpg)" "largefile2" $HOME/archive.tar /tmp "gpg --decrypt" &
21 # appendArgTar "$(cat /tmp/largefile3.gpg)" "largefile3" $HOME/archive.tar /tmp "gpg --decrypt" &
25 local FN
="${FUNCNAME[0]}";
26 #yell "DEBUG:STATUS:$FN:Finished appendArgTar()."
29 if ! [ -z "$2" ]; then FILENAME
="$2"; else yell
"ERROR:$FN:Not enough arguments."; exit 1; fi
31 # Check tar path is a file
32 if [ -f "$3" ]; then TAR_PATH
="$3"; else yell
"ERROR:$FN:Tar archive arg not a file."; exit 1; fi
35 if ! [ -z "$4" ]; then TMP_DIR
="$4"; else yell
"ERROR:$FN:No temporary working dir set."; exit 1; fi
38 if ! [ -z "$5" ]; then CMD1
="$5"; else CMD1
="tee /dev/null "; fi # command string 1
39 if ! [ -z "$6" ]; then CMD2
="$6"; else CMD2
="tee /dev/null "; fi # command string 2
40 if ! [ -z "$7" ]; then CMD3
="$7"; else CMD3
="tee /dev/null "; fi # command string 3
41 if ! [ -z "$8" ]; then CMD4
="$8"; else CMD4
="tee /dev/null "; fi # command string 4
44 # yell "DEBUG:STATUS:$FN:CMD1:$CMD1"
45 # yell "DEBUG:STATUS:$FN:CMD2:$CMD2"
46 # yell "DEBUG:STATUS:$FN:CMD3:$CMD3"
47 # yell "DEBUG:STATUS:$FN:CMD4:$CMD4"
48 # yell "DEBUG:STATUS:$FN:FILENAME:$FILENAME"
49 # yell "DEBUG:STATUS:$FN:TAR_PATH:$TAR_PATH"
50 # yell "DEBUG:STATUS:$FN:TMP_DIR:$TMP_DIR"
52 # Write to temporary working dir
53 echo "$1" |
$CMD1 |
$CMD2 |
$CMD3 |
$CMD4 > "$TMP_DIR"/"$FILENAME";
56 try
tar --append --directory="$TMP_DIR" --file="$TAR_PATH" "$FILENAME";
57 #yell "DEBUG:STATUS:$FN:Finished appendArgTar()."
58 } # Append Bash var to file appended to Tar archive
60 #==BEGIN sample code==
61 myTar
="/tmp/$(date +%s).tar";
62 try
tar --create --file="$myTar" --files-from=/dev
/null
;
63 myVar
="how are you doing?"
64 appendArgTar
"$myVar" "$(date +%s)..LOUD_insidetar" "$myTar" "/dev/shm" "tee /tmp/$(date +%s)..outsidetar " "tr '[:lower:]' '[:upper:]'"
67 # Author: Steven Baltakatei Sandoval