-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt
executable file
·57 lines (51 loc) · 1.31 KB
/
decrypt
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
#!/bin/bash
# Decrypt an gpg encrypt file.
# Name:
# decrypt
#
# Usage:
# decrypt gpgFile
# gpgFile - encrypted file (encrypted using encrypt or gpg command)
#
# See also:
# encrypt
#
# yaswant.pradhan
# -----------------------------------------------------------------------------
[ -z "$1" ] && echo -e "usage:\t${0##*/} <gpgfile>\n" && exit 1
Extn0=${1##*.}
Base0=${1%.*}
# validate gpg file extension
[[ "$Extn0" != 'gpg' ]] && { printf "%s\n" "$1 is not a gpg file"; exit 0; }
# does tar extraction required?
Extn1=${Base0##*.}
Base1=${Base0%.*}
# Validate existing file/directory
if [ -e "$Base1" ]; then
printf "%s\n" "Warning: A file/dir with name '$Base1' already exists in path"
read -rp "Would you like to continue/overwrite? y|N: " x
[[ $x != y ]] && printf "Aborted." && exit 0
fi
# decrypt [and extract]
case $Extn1 in
tar)
printf "\n%s" "Extract dir '$Base1'..."
gpg --decrypt "$1" 2>/dev/null | tar -x
printf "done.\n"
;;
tgz)
printf "\n%s" "Extract dir '$Base1'..."
gpg --decrypt "$1" 2>/dev/null | tar -xz
printf "done.\n"
;;
tbz|tbz2)
printf "\n%s" "Extract dir '$Base1'..."
gpg --decrypt "$1" 2>/dev/null | tar -xj
printf "done.\n"
;;
*)
printf "\n%s" "Restore file '$Base0'..."
gpg "$1" 2>/dev/null
printf "done.\n"
;;
esac