fix(unitproc):appendArgTar:Fix sample code
[BK-2020-03.git] / unitproc / bktemp-appendFileTar
1 #!/bin/bash
2
3 # Desc: Template function to write data supplied as argument
4
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 $*"; }
8 appendFileTar(){
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]...)
11 # Version: 1.0.1
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
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" &
22 # Depends: bash 5
23
24 # Save function name
25 local FN="${FUNCNAME[0]}";
26 #yell "DEBUG:STATUS:$FN:Finished 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 TAR_PATH="$3"; else yell "ERROR:$FN:Tar archive arg not a file."; exit 1; fi
32 # Check temp dir arg
33 if ! [ -z "$4" ]; then TMP_DIR="$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="tee /dev/null "; fi # command string 1
36 if ! [ -z "$6" ]; then CMD2="$6"; else CMD2="tee /dev/null "; fi # command string 2
37 if ! [ -z "$7" ]; then CMD3="$7"; else CMD3="tee /dev/null "; fi # command string 3
38 if ! [ -z "$8" ]; then CMD4="$8"; else CMD4="tee /dev/null "; fi # command string 4
39 # # Debug
40 # yell "DEBUG:STATUS:$FN:CMD1:$CMD1"
41 # yell "DEBUG:STATUS:$FN:CMD2:$CMD2"
42 # yell "DEBUG:STATUS:$FN:CMD3:$CMD3"
43 # yell "DEBUG:STATUS:$FN:CMD4:$CMD4"
44 # yell "DEBUG:STATUS:$FN:FILENAME:$FILENAME"
45 # yell "DEBUG:STATUS:$FN:TAR_PATH:$TAR_PATH"
46 # yell "DEBUG:STATUS:$FN:TMP_DIR:$TMP_DIR"
47
48 # Write to temporary working dir
49 cat "$1" | $CMD1 | $CMD2 | $CMD3 | $CMD4 > "$TMP_DIR"/"$FILENAME";
50
51 # Append to tar
52 try tar --append --directory="$TMP_DIR" --file="$TAR_PATH" "$FILENAME";
53 #yell "DEBUG:STATUS:$FN:Finished appendFileTar()."
54 } # Append file to Tar archive
55
56 #==BEGIN sample code==
57 myTar="/tmp/$(date +%s).tar";
58 try tar --create --file="$myTar" --files-from=/dev/null ;
59 myFile="/tmp/$(date +%s)..original"
60 echo "how are you doing?" > "$myFile"
61 appendFileTar "$myFile" "$(date +%s)..LOUD_insidetar" "$myTar" "/dev/shm" "tee /tmp/$(date +%s)..outsidetar " "tr '[:lower:]' '[:upper:]'"
62 #==END sample code==
63
64 # Author: Steven Baltakatei Sandoval
65 # License: GPLv3+