codekingpro/portable-devtools
114k
1#!/bin/sh2 3# Bzgrep wrapped for bzip2, 4# adapted from zgrep by Philippe Troin <phil@fifi.org> for Debian GNU/Linux.5## zgrep notice:6## zgrep -- a wrapper around a grep program that decompresses files as needed7## Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>8 9PATH="/usr/bin:$PATH"; export PATH10 11prog=`echo $0 | sed 's|.*/||'`12case "$prog" in13 *egrep) grep=${EGREP-egrep} ;;14 *fgrep) grep=${FGREP-fgrep} ;;15 *) grep=${GREP-grep} ;;16esac17pat=""18while test $# -ne 0; do19 case "$1" in20 -e | -f) opt="$opt $1"; shift; pat="$1"21 if test "$grep" = grep; then # grep is buggy with -e on SVR422 grep=egrep23 fi;;24 -A | -B) opt="$opt $1 $2"; shift;;25 -*) opt="$opt $1";;26 *) if test -z "$pat"; then27 pat="$1"28 else29 break;30 fi;;31 esac32 shift33done34 35if test -z "$pat"; then36 echo "grep through bzip2 files"37 echo "usage: $prog [grep_options] pattern [files]"38 exit 139fi40 41list=042silent=043op=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`44case "$op" in45 *l*) list=146esac47case "$op" in48 *h*) silent=149esac50 51if test $# -eq 0; then52 bzip2 -cdfq | $grep $opt "$pat"53 exit $?54fi55 56res=057for i do58 if test -f "$i"; then :; else if test -f "$i.bz2"; then i="$i.bz2"; fi; fi59 if test $list -eq 1; then60 bzip2 -cdfq "$i" | $grep $opt "$pat" 2>&1 > /dev/null && echo $i61 r=$?62 elif test $# -eq 1 -o $silent -eq 1; then63 bzip2 -cdfq "$i" | $grep $opt "$pat"64 r=$?65 else66 j=$(echo "$i" | sed 's/\\/&&/g;s/|/\\&/g;s/&/\\&/g')67 j=`printf "%s" "$j" | tr '\n' ' '`68 # A trick adapted from69 # https://groups.google.com/forum/#!original/comp.unix.shell/x1345iu10eg/Nn1n-1r1uU0J70 # that has the same effect as the following bash code:71 # bzip2 -cdfq "$i" | $grep $opt "$pat" | sed "s|^|${j}:|"72 # r=${PIPESTATUS[1]}73 exec 3>&174 eval `75 exec 4>&1 >&3 3>&-76 {77 bzip2 -cdfq "$i" 4>&-78 } | {79 $grep $opt "$pat" 4>&-; echo "r=$?;" >&480 } | sed "s|^|${j}:|"81 `82 fi83 test "$r" -ne 0 && res="$r"84done85exit $res86 