-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunpackme.sh
executable file
·101 lines (89 loc) · 2.18 KB
/
unpackme.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
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
R=0
V=0
ix=0
i_s=0
############### * FLAGS CHECKS
# Verbose messages
verbose_no_file() {
if [ $V -eq 1 ] ; then echo "The '$1' does not exist - ignoring."; fi
}
verbose_bad_format() {
if [ $V -eq 1 ] ; then echo "The '$1' does not have proper format - ignoring."; fi
}
verbose_ok() {
if [ $V -eq 1 ] ; then echo "The '$1' is decompressing .."; fi
}
############### * F * ########################
# Main when file
main_f() {
for FILE in $ARCHS
do
if [ -d $FILE ] ; then
dict_f $FILE
elif [ ! -f $FILE ] ; then
ix=$(( $ix+1 ))
verbose_no_file $FILE
else
ft_ch $FILE
decompr $TYPE $FILE
fi
done
}
# Main for dictionaries
main_dd() {
for FILE in $ARCHS
do
ft_ch $FILE
decompr $TYPE $FILE
done
}
# File type check
ft_ch() {
TYPE=$(file -b $1 | cut -d ' ' -f 1)
}
# Dictionary function , checking if recursive
dict_f() {
if [ $R -eq 1 ] ; then
ARCHS=$(find $FILE -type f )
main_dd $ARCHS
else
ARCHS=$(find $FILE -maxdepth 1 -type f)
main_dd $ARCHS
fi
}
# Decompress cases $1 arg-type, $2 - file-name
decompr() {
case "$1" in
"Zip" ) unzip -uqq $2 && verbose_ok $2 && i_s=$(( $i_s+1 )) ;;
"bzip2" ) bunzip2 -kfd $2 && verbose_ok $2 && i_s=$(( $i_s+1 )) ;;
"gzip" ) gunzip -fkdq $2 && verbose_ok $2 && i_s=$(( $i_s+1 )) ;;
"compress'd" ) mv $2 $2.Z && uncompress -df $2.Z && verbose_ok $2 && i_s=$(( $i_s+1 )) ;;
* ) ix=$(( $ix+1 )) && verbose_bad_format $2 ;;
esac
}
################## * BEGIN * ###################
# Banner
if [ $# -eq 0 ] ; then
echo ""
echo "Unpackme 2022 "
echo -e "Please fallow the syntax \n"
echo "unpack [-r] [-v] file [file...]"
echo " -r traverse all provided path"
echo -e " -v verbose\n"
exit 1
fi
# Setting values for flags
i=0
for each in $@
do
i=$(( i+1 ))
if [ $each == -v ] ; then V=1 ; fi
if [ $each == -r ] ; then R=1 ; fi
done
# Throwing away -v and -r if exist from all archives
ARCHS=$(echo $* | sed 's/\-r//' | sed 's/\-v//')
main_f
# Counters
echo "Ignored $ix file(s)."
echo "Decompressed $i_s archive(s)."