fix(user/zeropad.sh):Only zero-pad files with leading digits
[BK-2020-03.git] / unitproc / bkt-appendFileTar
CommitLineData
f7df3da5 1#!/bin/bash
3621642d 2# Desc: Append [processed] file to tar
f7df3da5
SBS
3
4yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
5die() { yell "$*"; exit 111; }
6try() { "$@" || die "cannot $*"; }
7appendFileTar(){
3621642d
SBS
8 # Desc: Appends [processed] file to tar
9 # Usage: appendFileTar [file path] [name of file to be inserted] [tar path] [temp dir] ([process cmd])
60a186e8 10 # Version: 2.0.1
f7df3da5
SBS
11 # Input: arg1: path of file to be (processed and) written
12 # arg2: name to use for file inserted into tar
13 # arg3: tar archive path (must exist first)
14 # arg4: temporary working dir
3621642d 15 # arg5: (optional) command string to process file (ex: "gpsbabel -i nmea -f - -o kml -F - ")
f7df3da5
SBS
16 # Output: file written to disk
17 # Example: decrypt multiple large files in parallel
4bc2baeb
SBS
18 # appendFileTar /tmp/largefile1.gpg "largefile1" $HOME/archive.tar /tmp "gpg --decrypt" &
19 # appendFileTar /tmp/largefile2.gpg "largefile2" $HOME/archive.tar /tmp "gpg --decrypt" &
20 # appendFileTar /tmp/largefile3.gpg "largefile3" $HOME/archive.tar /tmp "gpg --decrypt" &
3621642d 21 # Depends: bash 5.0.3, tar 1.30, cat 8.30, yell()
c1b629d0 22 local fn fileName tarPath tmpDir
f7df3da5
SBS
23
24 # Save function name
c1b629d0 25 fn="${FUNCNAME[0]}";
3621642d 26 #yell "DEBUG:STATUS:$fn:Started appendFileTar()."
f7df3da5
SBS
27
28 # Set file name
c1b629d0 29 if ! [ -z "$2" ]; then fileName="$2"; else yell "ERROR:$fn:Not enough arguments."; exit 1; fi
f7df3da5 30 # Check tar path is a file
c1b629d0 31 if [ -f "$3" ]; then tarPath="$3"; else yell "ERROR:$fn:Tar archive arg not a file:$3"; exit 1; fi
f7df3da5 32 # Check temp dir arg
c1b629d0 33 if ! [ -z "$4" ]; then tmpDir="$4"; else yell "ERROR:$fn:No temporary working dir set."; exit 1; fi
f7df3da5 34 # Set command strings
3621642d 35 if ! [ -z "$5" ]; then cmd1="$5"; else cmd1="cat "; fi # command string
b8b1e3a2
SBS
36
37 # Input command string
3621642d 38 cmd0="cat \"\$1\"";
f7df3da5
SBS
39
40 # Write to temporary working dir
3621642d 41 eval "$cmd0 | $cmd1" > "$tmpDir"/"$fileName";
f7df3da5
SBS
42
43 # Append to tar
c1b629d0
SBS
44 try tar --append --directory="$tmpDir" --file="$tarPath" "$fileName";
45 #yell "DEBUG:STATUS:$fn:Finished appendFileTar()."
60a186e8 46} # Append [processed] file to Tar archive
f7df3da5
SBS
47
48#==BEGIN sample code==
4bc2baeb 49myTar="/tmp/$(date +%s).tar";
c1b629d0 50myTmpDir="/dev/shm";
4bc2baeb 51try tar --create --file="$myTar" --files-from=/dev/null ;
c1b629d0
SBS
52myFile="/tmp/$(date +%s)..original";
53echo "how are you doing?" > "$myFile";
3621642d 54appendFileTar "$myFile" "$(date +%s)..LOUD_insidetar" "$myTar" "$myTmpDir" "tee /tmp/$(date +%s)..outsidetar | tr '[:lower:]' '[:upper:]'" && \
c1b629d0 55 echo "Inserted a processed version of $myFile into $myTar using temp dir $myTmpDir ";
f7df3da5
SBS
56#==END sample code==
57
58# Author: Steven Baltakatei Sandoval
59# License: GPLv3+