-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdctar.sh
executable file
·89 lines (80 loc) · 2.29 KB
/
dctar.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env sh
# Copyright 2019-2022 (c) all rights reserved by S D Rausty; Please see LICENSE
# https://sdrausty.github.io hosted courtesy https://pages.github.com
# File ` dctar.sh ` deletes corrupt *.tar.gz files.
################################################################################
set -eu
_MAINDCTAR_ () {
_PRINTPTF_
if [ "$1" = 0 ] # the first agrurment equals 0
then # process
if [ "${PWD##*/}" = tarballs ]
then
LTYPE="$(ls)" || _PRINTCPF_
_PTGS_
else
_PRINTCPF_
fi
elif [ "$1" = ls ] # finds and removes corrupt tarballs with ls; depth 1
then
LTYPE="$(ls *.tar.gz)" || _PRINTCPF_
_PTGS_
elif [ "$1" = lsf ] # finds and removes corrupt tarballs with ls; depth 2
then
LTYPE="$(ls -d -1 ./**/*.tar.gz)" || _PRINTCPF_
_PTGS_
elif [ "$1" = find ] # finds and removes corrupt tarballs with find; depth unlimited
then
LTYPE="$(find . -type f -name "*.tar.gz")" || _PRINTCPF_
_PTGS_
elif [ "$1" = find2 ] # finds and removes corrupt tarballs with find; depth 2
then
LTYPE="$(find . -maxdepth 2 -type f -name "*.tar.gz")" || _PRINTCPF_
_PTGS_
elif [ "$1" = find3 ] # finds and removes corrupt tarballs with find; depth 3
then
LTYPE="$(find . -maxdepth 3 -type f -name "*.tar.gz")" || _PRINTCPF_
_PTGS_
else
_PTG_ "$1"
fi
_PRINTMDONE_
}
_PRINTCPF_ () {
printf "%s\\n" "Cannot process *.tar.gz files!"
}
_PRINTDONE_ () {
printf "DONE\\n"
}
_PRINTMDONE_ () {
printf "%s\\n" "Processing *.tar.gz files: DONE"
printf "\033]2;%sDONE\007" "Processing *.tar.gz files: "
}
_PRINTHDCT_ () {
printf "%s\\n%s\\n" "Options for \` ${0##*/} \` are:" "0 see \` cat ${0##*/} \` for more information"
grep -w "elif \[" "$0" | awk '{print $5" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16}'
printf "%s\\n" "The script \` ${0##*/} \` should be run with an option as \` ${0##*/} \` removes *.tar.gz files as requested through options."
}
_PRINTPTF_ () {
printf "%s\\n" "Processing *.tar.gz files: "
printf "\033]2;%sOK\007" "Processing *.tar.gz files: "
}
_PTGS_ () { # process and remove *.tar.gz files with errors
for FNAME in $LTYPE
do
printf "%s" "Processing file $FNAME: "
if ! tar tf "$FNAME" 1>/dev/null
then
rm -f "$FNAME"
fi
printf "%s\\n" "DONE"
done
}
_PTG_ () { # process and remove a *.tar.gz file with errors
if ! tar tf "$1" 1>/dev/null
then
rm -f "$1"
fi
}
[ -z "${1:-}" ] && _PRINTHDCT_ || _MAINDCTAR_ "$@"
# dctar.sh EOF