Commit | Line | Data |
---|---|---|
0f8ac166 SBS |
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 | appendArgTar(){ | |
9 | # Desc: Writes first argument to temporary file with arguments as options, then appends file to tar | |
71d6b5c9 | 10 | # Usage: appendArgTar "$(echo "Data to be written.")" [name of file to be inserted] [tar path] [temp dir] ([cmd1] [cmd2] [cmd3] [cmd4]...) |
49c107c6 | 11 | # Version: 1.0.5 |
0f8ac166 SBS |
12 | # Input: arg1: data to be written |
13 | # arg2: file name of file to be 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 | # appendArgTar "$(cat /tmp/largefile1.gpg)" "largefile1" $HOME/archive.tar /tmp "gpg --decrypt" & | |
20 | # appendArgTar "$(cat /tmp/largefile2.gpg)" "largefile2" $HOME/archive.tar /tmp "gpg --decrypt" & | |
21 | # appendArgTar "$(cat /tmp/largefile3.gpg)" "largefile3" $HOME/archive.tar /tmp "gpg --decrypt" & | |
49c107c6 | 22 | # Depends: bash 5, tar 1, yell() |
f0531598 | 23 | # Ref/Attrib: Using 'eval' to construct command strings https://askubuntu.com/a/476533 |
0f8ac166 | 24 | |
49c107c6 SBS |
25 | local fn fileName tarPath tmpDir cmd0 cmd1 cmd2 cmd3 cmd4 |
26 | ||
4dad1c36 | 27 | # Save function name |
49c107c6 SBS |
28 | fn="${FUNCNAME[0]}"; |
29 | #yell "DEBUG:STATUS:$fn:Finished appendArgTar()." | |
4dad1c36 | 30 | |
0f8ac166 | 31 | # Set file name |
49c107c6 | 32 | if ! [ -z "$2" ]; then fileName="$2"; else yell "ERROR:$fn:Not enough arguments."; exit 1; fi |
0f8ac166 SBS |
33 | |
34 | # Check tar path is a file | |
49c107c6 | 35 | if [ -f "$3" ]; then tarPath="$3"; else yell "ERROR:$fn:Tar archive arg not a file."; exit 1; fi |
0f8ac166 SBS |
36 | |
37 | # Check temp dir arg | |
49c107c6 | 38 | if ! [ -z "$4" ]; then tmpDir="$4"; else yell "ERROR:$fn:No temporary working dir set."; exit 1; fi |
0f8ac166 SBS |
39 | |
40 | # Set command strings | |
49c107c6 SBS |
41 | if ! [ -z "$5" ]; then cmd1="$5"; else cmd1="tee /dev/null "; fi # command string 1 |
42 | if ! [ -z "$6" ]; then cmd2="$6"; else cmd2="tee /dev/null "; fi # command string 2 | |
43 | if ! [ -z "$7" ]; then cmd3="$7"; else cmd3="tee /dev/null "; fi # command string 3 | |
44 | if ! [ -z "$8" ]; then cmd4="$8"; else cmd4="tee /dev/null "; fi # command string 4 | |
0f8ac166 | 45 | |
f0531598 | 46 | # Input command |
49c107c6 | 47 | cmd0="echo \"\$1\"" |
f0531598 | 48 | |
4dad1c36 | 49 | # # Debug |
49c107c6 SBS |
50 | # yell "DEBUG:STATUS:$fn:cmd0:$cmd0" |
51 | # yell "DEBUG:STATUS:$fn:cmd1:$cmd1" | |
52 | # yell "DEBUG:STATUS:$fn:cmd2:$cmd2" | |
53 | # yell "DEBUG:STATUS:$fn:cmd3:$cmd3" | |
54 | # yell "DEBUG:STATUS:$fn:cmd4:$cmd4" | |
55 | # yell "DEBUG:STATUS:$fn:fileName:$fileName" | |
56 | # yell "DEBUG:STATUS:$fn:tarPath:$tarPath" | |
57 | # yell "DEBUG:STATUS:$fn:tmpDir:$tmpDir" | |
4dad1c36 | 58 | |
0f8ac166 | 59 | # Write to temporary working dir |
49c107c6 | 60 | eval "$cmd0 | $cmd1 | $cmd2 | $cmd3 | $cmd4" > "$tmpDir"/"$fileName"; |
0f8ac166 SBS |
61 | |
62 | # Append to tar | |
49c107c6 SBS |
63 | try tar --append --directory="$tmpDir" --file="$tarPath" "$fileName"; |
64 | #yell "DEBUG:STATUS:$fn:Finished appendArgTar()." | |
0f8ac166 SBS |
65 | } # Append Bash var to file appended to Tar archive |
66 | ||
67 | #==BEGIN sample code== | |
0089d651 | 68 | myTar="/tmp/$(date +%s).tar"; |
49c107c6 | 69 | myTmpDir="/dev/shm" |
0089d651 SBS |
70 | try tar --create --file="$myTar" --files-from=/dev/null ; |
71 | myVar="how are you doing?" | |
49c107c6 SBS |
72 | appendArgTar "$myVar" "$(date +%s)..LOUD_insidetar" "$myTar" "$myTmpDir" "tee /tmp/$(date +%s)..outsidetar " "tr '[:lower:]' '[:upper:]'" && \ |
73 | echo "Created $myTar using $myTmpDir as temp working dir." | |
0f8ac166 SBS |
74 | #==END sample code== |
75 | ||
76 | # Author: Steven Baltakatei Sandoval | |
77 | # License: GPLv3+ |