style(unitproc):appendFileTar():VAR_NAME to varName
[BK-2020-03.git] / unitproc / bktemp-appendFileTar
CommitLineData
f7df3da5
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 $*"; }
8appendFileTar(){
9 # Desc: Processes first file and then appends to tar
10 # Usage: appendFileTar [file path] [name of file to be inserted] [tar path] [temp dir] ([cmd1] [cmd2] [cmd3] [cmd4]...)
c1b629d0 11 # Version: 1.0.3
f7df3da5
SBS
12 # Input: arg1: path of file to be (processed and) written
13 # arg2: name to use for file 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
4bc2baeb
SBS
19 # appendFileTar /tmp/largefile1.gpg "largefile1" $HOME/archive.tar /tmp "gpg --decrypt" &
20 # appendFileTar /tmp/largefile2.gpg "largefile2" $HOME/archive.tar /tmp "gpg --decrypt" &
21 # appendFileTar /tmp/largefile3.gpg "largefile3" $HOME/archive.tar /tmp "gpg --decrypt" &
c1b629d0
SBS
22 # Depends: bash 5, tar 1, yell()
23
24 local fn fileName tarPath tmpDir
f7df3da5
SBS
25
26 # Save function name
c1b629d0
SBS
27 fn="${FUNCNAME[0]}";
28 #yell "DEBUG:STATUS:$fn:Finished appendFileTar()."
f7df3da5
SBS
29
30 # Set file name
c1b629d0 31 if ! [ -z "$2" ]; then fileName="$2"; else yell "ERROR:$fn:Not enough arguments."; exit 1; fi
f7df3da5 32 # Check tar path is a file
c1b629d0 33 if [ -f "$3" ]; then tarPath="$3"; else yell "ERROR:$fn:Tar archive arg not a file:$3"; exit 1; fi
f7df3da5 34 # Check temp dir arg
c1b629d0 35 if ! [ -z "$4" ]; then tmpDir="$4"; else yell "ERROR:$fn:No temporary working dir set."; exit 1; fi
f7df3da5 36 # Set command strings
c1b629d0
SBS
37 if ! [ -z "$5" ]; then cmd1="$5"; else cmd1="cat "; fi # command string 1
38 if ! [ -z "$6" ]; then cmd2="$6"; else cmd2="cat "; fi # command string 2
39 if ! [ -z "$7" ]; then cmd3="$7"; else cmd3="cat "; fi # command string 3
40 if ! [ -z "$8" ]; then cmd4="$8"; else cmd4="cat "; fi # command string 4
b8b1e3a2
SBS
41
42 # Input command string
c1b629d0 43 cmd0="cat \"\$1\""
b8b1e3a2 44
f7df3da5 45 # # Debug
c1b629d0
SBS
46 # yell "DEBUG:STATUS:$fn:cmd0:$cmd0"
47 # yell "DEBUG:STATUS:$fn:cmd1:$cmd1"
48 # yell "DEBUG:STATUS:$fn:cmd2:$cmd2"
49 # yell "DEBUG:STATUS:$fn:cmd3:$cmd3"
50 # yell "DEBUG:STATUS:$fn:cmd4:$cmd4"
51 # yell "DEBUG:STATUS:$fn:fileName:$fileName"
52 # yell "DEBUG:STATUS:$fn:tarPath:$tarPath"
53 # yell "DEBUG:STATUS:$fn:tmpDir:$tmpDir"
f7df3da5
SBS
54
55 # Write to temporary working dir
c1b629d0 56 eval "$cmd0 | $cmd1 | $cmd2 | $cmd3 | $cmd4" > "$tmpDir"/"$fileName";
f7df3da5
SBS
57
58 # Append to tar
c1b629d0
SBS
59 try tar --append --directory="$tmpDir" --file="$tarPath" "$fileName";
60 #yell "DEBUG:STATUS:$fn:Finished appendFileTar()."
4bc2baeb 61} # Append file to Tar archive
f7df3da5
SBS
62
63#==BEGIN sample code==
4bc2baeb 64myTar="/tmp/$(date +%s).tar";
c1b629d0 65myTmpDir="/dev/shm";
4bc2baeb 66try tar --create --file="$myTar" --files-from=/dev/null ;
c1b629d0
SBS
67myFile="/tmp/$(date +%s)..original";
68echo "how are you doing?" > "$myFile";
69appendFileTar "$myFile" "$(date +%s)..LOUD_insidetar" "$myTar" "$myTmpDir" "tee /tmp/$(date +%s)..outsidetar " "tr '[:lower:]' '[:upper:]'" && \
70 echo "Inserted a processed version of $myFile into $myTar using temp dir $myTmpDir ";
f7df3da5
SBS
71#==END sample code==
72
73# Author: Steven Baltakatei Sandoval
74# License: GPLv3+