feat(user/bk-copy-rand-music):Optimize file processing
[BK-2020-03.git] / unitproc / bkt-appendFileTar
... / ...
CommitLineData
1#!/bin/bash
2# Desc: Append [processed] file to tar
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(){
8 # Desc: Appends [processed] file to tar
9 # Usage: appendFileTar [file path] [name of file to be inserted] [tar path] [temp dir] ([process cmd])
10 # Version: 2.0.1
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
15 # arg5: (optional) command string to process file (ex: "gpsbabel -i nmea -f - -o kml -F - ")
16 # Output: file written to disk
17 # Example: decrypt multiple large files in parallel
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" &
21 # Depends: bash 5.0.3, tar 1.30, cat 8.30, yell()
22 local fn fileName tarPath tmpDir
23
24 # Save function name
25 fn="${FUNCNAME[0]}";
26 #yell "DEBUG:STATUS:$fn:Started appendFileTar()."
27
28 # Set file name
29 if ! [ -z "$2" ]; then fileName="$2"; else yell "ERROR:$fn:Not enough arguments."; exit 1; fi
30 # Check tar path is a file
31 if [ -f "$3" ]; then tarPath="$3"; else yell "ERROR:$fn:Tar archive arg not a file:$3"; exit 1; fi
32 # Check temp dir arg
33 if ! [ -z "$4" ]; then tmpDir="$4"; else yell "ERROR:$fn:No temporary working dir set."; exit 1; fi
34 # Set command strings
35 if ! [ -z "$5" ]; then cmd1="$5"; else cmd1="cat "; fi # command string
36
37 # Input command string
38 cmd0="cat \"\$1\"";
39
40 # Write to temporary working dir
41 eval "$cmd0 | $cmd1" > "$tmpDir"/"$fileName";
42
43 # Append to tar
44 try tar --append --directory="$tmpDir" --file="$tarPath" "$fileName";
45 #yell "DEBUG:STATUS:$fn:Finished appendFileTar()."
46} # Append [processed] file to Tar archive
47
48#==BEGIN sample code==
49myTar="/tmp/$(date +%s).tar";
50myTmpDir="/dev/shm";
51try tar --create --file="$myTar" --files-from=/dev/null ;
52myFile="/tmp/$(date +%s)..original";
53echo "how are you doing?" > "$myFile";
54appendFileTar "$myFile" "$(date +%s)..LOUD_insidetar" "$myTar" "$myTmpDir" "tee /tmp/$(date +%s)..outsidetar | tr '[:lower:]' '[:upper:]'" && \
55 echo "Inserted a processed version of $myFile into $myTar using temp dir $myTmpDir ";
56#==END sample code==
57
58# Author: Steven Baltakatei Sandoval
59# License: GPLv3+