mirror of https://github.com/tLDP/LDP
40 lines
832 B
Bash
40 lines
832 B
Bash
#!/bin/bash
|
|
# bin-grep.sh: Locates matching strings in a binary file.
|
|
|
|
# A "grep" replacement for binary files.
|
|
# Similar effect to "grep -a"
|
|
|
|
E_BADARGS=65
|
|
E_NOFILE=66
|
|
|
|
if [ $# -ne 2 ]
|
|
then
|
|
echo "Usage: `basename $0` search_string filename"
|
|
exit $E_BADARGS
|
|
fi
|
|
|
|
if [ ! -f "$2" ]
|
|
then
|
|
echo "File \"$2\" does not exist."
|
|
exit $E_NOFILE
|
|
fi
|
|
|
|
|
|
IFS=$'\012' # Per suggestion of Anton Filippov.
|
|
# was: IFS="\n"
|
|
for word in $( strings "$2" | grep "$1" )
|
|
# The "strings" command lists strings in binary files.
|
|
# Output then piped to "grep", which tests for desired string.
|
|
do
|
|
echo $word
|
|
done
|
|
|
|
# As S.C. points out, lines 23 - 30 could be replaced with the simpler
|
|
# strings "$2" | grep "$1" | tr -s "$IFS" '[\n*]'
|
|
|
|
|
|
# Try something like "./bin-grep.sh mem /bin/ls"
|
|
#+ to exercise this script.
|
|
|
|
exit 0
|