a1dc31208fddce40496b828ccb00c760761d9dd6
3 # Desc: Checks that a valid tar archive exists, creates one otherwise
6 yell
() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique
7 die
() { yell
"$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370
8 try
() { "$@" || die
"cannot $*"; } #o
10 #===BEGIN Declare local script functions===
12 # Desc: Checks that a valid tar archive exists, creates one otherwise
13 # Usage: checkMakeTar [ path ]
14 # Input: arg1: path of tar archive
15 # Output: exit code 0 (even if tar had to be created)
16 # Depends: try, tar, date
19 # Check if file is a valid tar archive
20 if tar --list --file="$PATH_TAR" 1>/dev
/null
2>&1; then
24 ## F1: Check if file exists
25 if [[ -f "$PATH_TAR" ]]; then
27 try
mv "$PATH_TAR" "$PATH_TAR""--broken--""$(date +%Y%m%dT%H%M%S)";
32 ## F2: Create tar archive, return 0
33 try
tar --create --file="$PATH_TAR" --files-from=/dev
/null
;
36 } # checks if arg1 is tar; creates one otherwise
37 #===END Declare local script functions===
39 #====BEGIN sample code====
40 myFile
="/tmp/$(date +%s)..tar"
41 yell
"$myFile doesn't yet exist."
42 if checkMakeTar
"$myFile"; then
43 yell
"checkMakeTar() function run and exited:$?";
45 yell
"checkMakeTar() function run and exited:$?";
48 if [[ -f "$myFile" ]]; then
49 yell
"Now exists :$myFile";
52 yell
"Does not exist:$myFile";
55 #====END sample code====