-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipscript.sh
86 lines (72 loc) · 2.05 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#Shell script to inject updated files into zip files without unzipping
#Jeremy Hill 8/5/2016
#!/bin/bash
main () {
# check we have the correct number of paramaters
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
usage 1
fi
# check args 1 and 2 are full paths
case "$1" in
/*)
echo "zipfile ok: $1"
;;
*)
echo "zipfile should be full path"
exit 2
esac
case "$2" in
/*)
echo "add file ok: $2"
;;
*)
echo "add file should be full path"
exit 2
esac
case "$3" in
/*)
echo "add path should be relative only, and not starting with . or .."
exit 2
;;
.*)
echo "add path should be relative only, and not starting with . or .."
exit 2
;;
*)
echo "add path ok: $3"
;;
esac
tmpdir=$(mktemp -d)
cd "$tmpdir"
if [ "$3" != "" ]; then
mkdir -p "$3"
cp "$2" "$3"
add="$3/${2##*/}"
else
cp "$2" .
add=${2##*/}
fi
zip -g "$1" "$add"
cd /
rm -rf "$tmpdir"
}
usage () {
# print out a quick help message and exit
cat <<EOD
Usage:
$THISSCRIPT zipfile addfile [addpath]
This script is only for single files
if you want multiples in a dir you need to run each one manually
You must use the full path for zipfile and addfile. The addfile
will be added into the zipfile in path addpath. e.g.
$THISSCRIPT /path/to/file.zip /path/to/addfile newfiles
...will add newfiles/addfile inside file.zip
If addpath is empty, addfile will be added into the root of zipfile.
EOD
# exit using parameter 1 as value else 0.
exit ${1:-0}
}
# set the basename of the script for the usage message
THISSCRIPT=${0##*/}
# call the main function
main "$@"