style(unitproc):timeDuration:Change VAR_NAMES to varNames
[BK-2020-03.git] / unitproc / bktemp-appendArgTar
... / ...
CommitLineData
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: appendArgTar "$(echo "Data to be written.")" [name of file to be inserted] [tar path] [temp dir] ([cmd1] [cmd2] [cmd3] [cmd4]...)
11 # Version: 1.0.4
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" &
22 # Depends: bash 5
23 # Ref/Attrib: Using 'eval' to construct command strings https://askubuntu.com/a/476533
24
25 # Save function name
26 local FN="${FUNCNAME[0]}";
27 #yell "DEBUG:STATUS:$FN:Finished appendArgTar()."
28
29 # Set file name
30 if ! [ -z "$2" ]; then FILENAME="$2"; else yell "ERROR:$FN:Not enough arguments."; exit 1; fi
31
32 # Check tar path is a file
33 if [ -f "$3" ]; then TAR_PATH="$3"; else yell "ERROR:$FN:Tar archive arg not a file."; exit 1; fi
34
35 # Check temp dir arg
36 if ! [ -z "$4" ]; then TMP_DIR="$4"; else yell "ERROR:$FN:No temporary working dir set."; exit 1; fi
37
38 # Set command strings
39 if ! [ -z "$5" ]; then CMD1="$5"; else CMD1="tee /dev/null "; fi # command string 1
40 if ! [ -z "$6" ]; then CMD2="$6"; else CMD2="tee /dev/null "; fi # command string 2
41 if ! [ -z "$7" ]; then CMD3="$7"; else CMD3="tee /dev/null "; fi # command string 3
42 if ! [ -z "$8" ]; then CMD4="$8"; else CMD4="tee /dev/null "; fi # command string 4
43
44 # Input command
45 CMD0="echo \"\$1\""
46
47 # # Debug
48 # yell "DEBUG:STATUS:$FN:CMD0:$CMD0"
49 # yell "DEBUG:STATUS:$FN:CMD1:$CMD1"
50 # yell "DEBUG:STATUS:$FN:CMD2:$CMD2"
51 # yell "DEBUG:STATUS:$FN:CMD3:$CMD3"
52 # yell "DEBUG:STATUS:$FN:CMD4:$CMD4"
53 # yell "DEBUG:STATUS:$FN:FILENAME:$FILENAME"
54 # yell "DEBUG:STATUS:$FN:TAR_PATH:$TAR_PATH"
55 # yell "DEBUG:STATUS:$FN:TMP_DIR:$TMP_DIR"
56
57 # Write to temporary working dir
58 eval "$CMD0 | $CMD1 | $CMD2 | $CMD3 | $CMD4" > "$TMP_DIR"/"$FILENAME";
59
60 # Append to tar
61 try tar --append --directory="$TMP_DIR" --file="$TAR_PATH" "$FILENAME";
62 #yell "DEBUG:STATUS:$FN:Finished appendArgTar()."
63} # Append Bash var to file appended to Tar archive
64
65#==BEGIN sample code==
66myTar="/tmp/$(date +%s).tar";
67try tar --create --file="$myTar" --files-from=/dev/null ;
68myVar="how are you doing?"
69appendArgTar "$myVar" "$(date +%s)..LOUD_insidetar" "$myTar" "/dev/shm" "tee /tmp/$(date +%s)..outsidetar " "tr '[:lower:]' '[:upper:]'"
70#==END sample code==
71
72# Author: Steven Baltakatei Sandoval
73# License: GPLv3+