-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipScript.sh
66 lines (57 loc) · 1.21 KB
/
zipScript.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
#!/bin/bash
# Script that tries to handle tar commands in an intelligent way
# Disclaimer: This is a demo for the case statement, not a production-ready script!
# Use: mytar dir file
# This will compress the directory dir into file
# Use: mytar filename
# This will extract the file
if [[ ! $1 ]]; then
echo "Need a file or directory as first argument" >&2
exit 1
fi
if [[ ! -e $1 ]]; then
echo "File or directory $1 not found." >&2
exit 2
fi
if [[ -d $1 ]]; then
# Is a directory: create archive
operation="c"
if [[ ! $2 ]]; then
echo "Need name of file or directory to create as second argument" >&2
exit 1
fi
tarfile="$2"
dir="$1"
else
# Is (probably) a file: try to extract
operation="x"
tarfile="$1"
dir=""
fi
case $tarfile in
*.tgz|*.gz|*.gzip)
zip="z"
echo "Using gzip" >&2;;
*.bz|*.bz2|*.bzip|*.bzip2)
zip="j"
echo "Using bzip2" >&2;;
*.Z)
zip="Z"
echo "Using compress" >&2;;
*.tar)
zip=""
echo "No compression used" >&2;;
*)
echo "Unknown extension: ${tarfile}" >&2
exit 3;;
esac
command="tar ${operation}${zip}f $tarfile"
if [[ $dir ]]; then
command="${command} $dir"
fi
if ! $command; then
echo "Error: tar exited with status $?" >&2
exit 4
fi
echo "Ok" >&2
exit 0