2/1/10

Grep replacement for binary files / Script

Example 10-7. A grep replacement for binary files

    #!/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` string filename"
     exit $E_BADARGS
   fi
  
   if [ ! -f "$2" ]
   then
     echo "File \"$2\" does not exist."
     exit $E_NOFILE
   fi 
  
  
   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, the above for-loop 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

 

<http://www.faqs.org/docs/abs/HTML/loops.html#FILEINFO>



No comments:

Quick HTTP to HTTPS - Apache2

There are several methods for redirecting your Apache-based website visitors who might type your servers URL using the plain (non-secure) HT...