1#! /bin/sh
2# zipgrep: searches the given zip members for a string or pattern
3# This shell script assumes that you have installed UnZip.
4
5pat=""
6opt=""
7while test $# -ne 0; do
8  case "$1" in
9  -e | -f) opt="$opt $1"; shift; pat="$1";;
10  -*)      opt="$opt $1";;
11   *)      if test -z "$pat"; then
12             pat="$1"
13           else
14             break;
15           fi;;
16  esac
17  shift
18done
19
20if 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
23  exit 1
24fi
25zipfile="$1"; shift
26
27list=0
28silent=0
29opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
30case "$opt" in
31  *l*) list=1; opt=`echo $opt | sed s/l//`
32esac
33case "$opt" in
34  *h*) silent=1
35esac
36if test -n "$opt"; then
37  opt="-$opt"
38fi
39
40res=0
41for i in `unzip -Z1 "$zipfile" ${1+"$@"}`; do
42  if test $list -eq 1; then
43
44    unzip -p-L "$zipfile" "$i" | grep -E $opt "$pat" > /dev/null && echo $i
45    r=$?
46  elif test $silent -eq 1; then
47
48    unzip -p-L "$zipfile" "$i" | grep -E $opt "$pat"
49    r=$?
50  else
51    unzip -p-L "$zipfile" "$i" | grep -E $opt "$pat" | sed "s|^|${i}:|"
52    r=$?
53  fi
54  test "$r" -ne 0 && res="$r"
55done
56exit $res
57