2
# zipgrep: searches the given zip members for a string or pattern
3
# This shell script assumes that you have installed UnZip.
3
# zipgrep: Use unzip and egrep to search the specified members of a
4
# Zip archive for a string or pattern. Search all members if no members
5
# are specified explicitly. The script attempts to handle egrep's "-h"
6
# and "-l" options internally.
8
# This script assumes that the desired "unzip" and "egrep" (and "sed")
9
# programs are on the user's PATH.
20
27
if test $# = 0; then
21
echo "usage: `basename $0` [egrep_options] pattern zipfile [members...]"
22
echo searches the given zip members for a string or pattern
28
echo usage: `basename "$0"` "[egrep_options] pattern zipfile [members...]"
29
echo Uses unzip and egrep to search the zip members for a string or pattern.
25
32
zipfile="$1"; shift
43
for i in `unzip -Z1 "$zipfile" ${1+"$@"}`; do
51
# Escape shell-special characters in "pat".
53
sed -e 's/\\\\/\\\\\\\\/g' -e 's/|/\\\|/g' -e 's/&/\\\&/g' `
55
# Use "unzip -Z1" to get a listing of the specified members from the
56
# specified archive. Escape any backslashes in a file name.
57
for i in `unzip -Z1 "$zipfile" ${1+"$@"} | sed -e 's/\\\\/\\\\\\\\/g' `; do
44
58
if test $list -eq 1; then
46
unzip -p-L "$zipfile" "$i" | egrep $opt "$pat" > /dev/null && echo $i
59
# "-l": Show only the archive member name, not the matching line(s).
60
unzip -p-L "$zipfile" "$i" | \
61
egrep $opt "$pat" > /dev/null && echo "$i"
48
63
elif test $silent -eq 1; then
50
unzip -p-L "$zipfile" "$i" | egrep $opt "$pat"
64
# "-h": Show only the matching line(s), not the archive member name.
65
# ("-s" in "opt" will silence "egrep", stopping all output.)
66
unzip -p-L "$zipfile" "$i" | \
53
unzip -p-L "$zipfile" "$i" | egrep $opt "$pat" | sed "s|^|${i}:|"
70
# Escape (or re-escape) shell-special characters in the archive
73
sed -e 's/\\\\/\\\\\\\\/g' -e 's/|/\\\|/g' -e 's/&/\\\&/g' `
75
# Globally, send fd 4 to stdout. In the pipeline, send normal
76
# stdout to fd 4, and send grep status to fd 3. Collect fd 3
80
( unzip -p-L "$zipfile" "$i" | \
81
egrep $opt "$pat" 1>&4 ; echo $? >&3 ) 4>&1 | \
82
sed "s|^|${i}:|" 1>&4 \
56
test "$r" -ne 0 && res="$r"
86
# Save the primary command status. (May be the grep status.)
88
# If this grep status was zero, set the global grep status to zero.
89
test "$status_grep" -eq 0 && status_grep_global=0
90
# If this grep status was not zero or one, exit now.
91
test "$status_grep" -gt 1 && exit "$status_grep"
95
# If "sts" is good (0), then exit with the global grep status.
96
# Else, when "sts" is bad, exit with the worst status we can find.
97
if test $sts -eq 0 ; then
98
exit $status_grep_global
100
if test "$status_grep" -gt 1 ; then