feat(unitproc):bkFreqWrite:Init script to process stdin
[BK-2020-03.git] / unitproc / bktemp-appendFileTar
... / ...
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 $*"; }
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]...)
11 # Version: 1.0.2
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
40 # Input command string
41 CMD0="cat \"\$1\""
42
43 # # Debug
44 # yell "DEBUG:STATUS:$FN:CMD0:$CMD0"
45 # yell "DEBUG:STATUS:$FN:CMD1:$CMD1"
46 # yell "DEBUG:STATUS:$FN:CMD2:$CMD2"
47 # yell "DEBUG:STATUS:$FN:CMD3:$CMD3"
48 # yell "DEBUG:STATUS:$FN:CMD4:$CMD4"
49 # yell "DEBUG:STATUS:$FN:FILENAME:$FILENAME"
50 # yell "DEBUG:STATUS:$FN:TAR_PATH:$TAR_PATH"
51 # yell "DEBUG:STATUS:$FN:TMP_DIR:$TMP_DIR"
52
53 # Write to temporary working dir
54 eval "$CMD0 | $CMD1 | $CMD2 | $CMD3 | $CMD4" > "$TMP_DIR"/"$FILENAME";
55
56 # Append to tar
57 try tar --append --directory="$TMP_DIR" --file="$TAR_PATH" "$FILENAME";
58 #yell "DEBUG:STATUS:$FN:Finished appendFileTar()."
59} # Append file to Tar archive
60
61#==BEGIN sample code==
62myTar="/tmp/$(date +%s).tar";
63try tar --create --file="$myTar" --files-from=/dev/null ;
64myFile="/tmp/$(date +%s)..original"
65echo "how are you doing?" > "$myFile"
66appendFileTar "$myFile" "$(date +%s)..LOUD_insidetar" "$myTar" "/dev/shm" "tee /tmp/$(date +%s)..outsidetar " "tr '[:lower:]' '[:upper:]'"
67#==END sample code==
68
69# Author: Steven Baltakatei Sandoval
70# License: GPLv3+