Need unix command file HELP!
Geoff Kuenning
geoff at desint.UUCP
Mon Feb 17 07:27:17 AEST 1986
In article <259 at hadron.UUCP> jsdy at hadron.UUCP (Joseph S. D. Yao) writes:
> text=`file "$file" | grep text`
Actually, there's still a gotcha here: it will pick up files named
'text.o', 'context.o', etc. (I know this from painful experience!). A
better way to detect text files is:
text=`file "$file" | grep ':.* text'`
or even
text=`file "$file" | grep 'text$'`
but I'd use the last only after checking the source of /bin/file to make
sure it always put 'text' last on the line.
Anyway, here is a TESTED (what a radical idea) shell script that:
(1) Searches only text files (2) Accepts the -l and -n switches of 'grep'
(3) remembers to put in /dev/null if -l is not specified, and (4) uses
xargs if it is available in /bin or /usr/bin. Both the BSD and the
System V variants have been tested (it adapts dynamically). It is an
improved version of a script that I sent to the original question-asker
more than a month ago.
Now, can we *please* move on to a new subject?
Geoff Kuenning
{hplabs,ihnp4}!trwrb!desint!geoff
------------------------cut here--------------------------
: Use /bin/sh
#!/bin/sh
#
# Locate a string in any (text) file, anywhere in the system.
#
# Usage:
#
# findstring [-l] [-n] [-g grep-program] [root-directory] search-string
#
# If the search-string contains semicolons, they should be backslashed,
# thus:
#
# findstring "break\;"
#
# The -l and -n switches are passed to the 'grep' program.
#
# The -g switch selects a different grep program; the default is 'grep'.
#
# WARNING: this command is very slow, and loads down the system
# quite heavily. The System V version opens every file in the
# system; the BSD version does that and also spawns at least one
# process for every file in the system and another for every
# text file. You can reduce the system load by a little bit by
# initiating the command from the root directory.
#
# Note: this is written to be portable to System V and BSD. It
# has only been tested on system V, though the BSD code was also
# tested there.
#
PATH=/bin:/usr/bin
ROOTDIR=/
grepargs=
nullfile=/dev/null
grep=grep
while :
do
case "X$1" in
X-l)
grepargs="$grepargs -l"
nullfile=
shift
;;
X-n)
grepargs="$grepargs -n"
shift
;;
X-g)
grep=$2
shift; shift
;;
X-*)
set illegal arguments - this will cause a message
break
;;
*)
break
;;
esac
done
if [ $# -gt 1 ]
then
ROOTDIR=$1
shift
fi
if [ $# -ne 1 ]
then
echo 'Usage: findstring [-l] [-n] [-g grep-program]' \
'[root-directory] search-string' 1>&2
exit 2
fi
#
# If you have UniSoft System V, test xargs to see if it has a bug by
# typing:
#
# echo a b c | xargs echo
#
# If you get nothing back, you have the bug. If you get "a b c",
# you don't.
#
# If you have the bug, you will have to disable the xargs variant
# below and make it run the non-xargs version. This is unfortunately
# *much* slower.
#
if [ -x /bin/xargs -o -x /usr/bin/xargs ]
then
#
# The system has xargs; use it
#
find $ROOTDIR -type f -print \
| xargs file \
| sed -n '/: .* text/s/: .*$//p' \
| xargs $grep $grepargs "$1" $nullfile
else
#
# Too bad, there's no xargs. We'll have to do it the hard way.
#
find $ROOTDIR -type f -exec file {} \; \
| sed -n '/: .* text/{
s/: .*$//
s;^;'"$grep $grepargs '$1' $nullfile"' ;p
}' \
| sh
fi
#
# Unfortunately, the grep's will return a nonzero status if they find
# nothing, so there isn't much point in returning their status.
#
exit 0
More information about the Comp.unix.wizards
mailing list