From: Steven Baltakatei Sandoval Date: Thu, 2 Jul 2020 18:17:21 +0000 (+0000) Subject: feat(unitproc):Renamed checkTar to checkMakeTar, add sample code X-Git-Tag: 0.2.0~10 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/949d90f84d8174208ac64231e4f35d182aaf3177?ds=inline;hp=--cc feat(unitproc):Renamed checkTar to checkMakeTar, add sample code --- 949d90f84d8174208ac64231e4f35d182aaf3177 diff --git a/unitproc/bktemp-checkMakeTar b/unitproc/bktemp-checkMakeTar new file mode 100644 index 0000000..a1dc312 --- /dev/null +++ b/unitproc/bktemp-checkMakeTar @@ -0,0 +1,55 @@ +#!/bin/bash + +# Desc: Checks that a valid tar archive exists, creates one otherwise + + +yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique +die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370 +try() { "$@" || die "cannot $*"; } #o + +#===BEGIN Declare local script functions=== +checkMakeTar() { + # Desc: Checks that a valid tar archive exists, creates one otherwise + # Usage: checkMakeTar [ path ] + # Input: arg1: path of tar archive + # Output: exit code 0 (even if tar had to be created) + # Depends: try, tar, date + local PATH_TAR="$1" + + # Check if file is a valid tar archive + if tar --list --file="$PATH_TAR" 1>/dev/null 2>&1; then + ## T1: return success + return 0; + else + ## F1: Check if file exists + if [[ -f "$PATH_TAR" ]]; then + ### T: Rename file + try mv "$PATH_TAR" "$PATH_TAR""--broken--""$(date +%Y%m%dT%H%M%S)"; + else + ### F: - + : + fi + ## F2: Create tar archive, return 0 + try tar --create --file="$PATH_TAR" --files-from=/dev/null; + return 0; + fi +} # checks if arg1 is tar; creates one otherwise +#===END Declare local script functions=== + +#====BEGIN sample code==== +myFile="/tmp/$(date +%s)..tar" +yell "$myFile doesn't yet exist." +if checkMakeTar "$myFile"; then + yell "checkMakeTar() function run and exited:$?"; +else + yell "checkMakeTar() function run and exited:$?"; +fi + +if [[ -f "$myFile" ]]; then + yell "Now exists :$myFile"; + ls -l "$myFile"; +else + yell "Does not exist:$myFile"; + ls -l "$myFile"; +fi +#====END sample code==== diff --git a/unitproc/bktemp-checkTar b/unitproc/bktemp-checkTar deleted file mode 100644 index 2485f3a..0000000 --- a/unitproc/bktemp-checkTar +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash - -# Desc: Checks that a valid tar archive exists, creates one otherwise - -# Yell, Die, Try Three-Fingered Claw technique -# Ref/Attrib: https://stackoverflow.com/a/25515370 -yell() { echo "$0: $*" >&2; } -die() { yell "$*"; exit 111; } -try() { "$@" || die "cannot $*"; } - -checkTar() { - # Desc: Checks that a valid tar archive exists, creates one otherwise - # Usage: checkTar [ path ] - # Input: arg1: path of tar archive - # Output: exit code 0: tar exists and is valid - # Depends: try, tar, date - local PATH_TAR="$1" - - # Check if file is a valid tar archive - if tar --list --file="$PATH_TAR" 1>/dev/null 2>&1; then - ## T1: return success - return 0; - else - ## F1: Check if file exists - if [[ -f "$PATH_TAR" ]]; then - ### T: Rename file - try mv "$PATH_TAR" "$PATH_TAR""--broken--""$(date +%Y%m%dT%H%M%S)"; - else - ### F: - - : - fi - ## F2: Create tar archive - try tar --create --file="$PATH_TAR" --files-from=/dev/null; - fi -} # checks if arg1 is tar; creates one otherwise