| 1 | #!/bin/bash |
| 2 | # wstrings.sh: "word-strings" (enhanced "strings" command) |
| 3 | # |
| 4 | # This script filters the output of "strings" by checking it |
| 5 | #+ against a standard word list file. |
| 6 | # This effectively eliminates gibberish and noise, |
| 7 | #+ and outputs only recognized words. |
| 8 | |
| 9 | # =========================================================== |
| 10 | # Standard Check for Script Argument(s) |
| 11 | ARGS=1 |
| 12 | E_BADARGS=85 |
| 13 | E_NOFILE=86 |
| 14 | |
| 15 | if [ $# -ne $ARGS ] |
| 16 | then |
| 17 | echo "Usage: `basename $0` filename" |
| 18 | exit $E_BADARGS |
| 19 | fi |
| 20 | |
| 21 | if [ ! -f "$1" ] # Check if file exists. |
| 22 | then |
| 23 | echo "File \"$1\" does not exist." |
| 24 | exit $E_NOFILE |
| 25 | fi |
| 26 | # =========================================================== |
| 27 | |
| 28 | |
| 29 | MINSTRLEN=3 # Minimum string length. |
| 30 | WORDFILE=/usr/share/dict/linux.words # Dictionary file. |
| 31 | # May specify a different word list file |
| 32 | #+ of one-word-per-line format. |
| 33 | # For example, the "yawl" word-list package, |
| 34 | # http://bash.deta.in/yawl-0.3.2.tar.gz |
| 35 | |
| 36 | |
| 37 | wlist=`strings "$1" | tr A-Z a-z | tr '[:space:]' Z | \ |
| 38 | tr -cs '[:alpha:]' Z | tr -s '\173-\377' Z | tr Z ' '` |
| 39 | |
| 40 | # Translate output of 'strings' command with multiple passes of 'tr'. |
| 41 | # "tr A-Z a-z" converts to lowercase. |
| 42 | # "tr '[:space:]'" converts whitespace characters to Z's. |
| 43 | # "tr -cs '[:alpha:]' Z" converts non-alphabetic characters to Z's, |
| 44 | #+ and squeezes multiple consecutive Z's. |
| 45 | # "tr -s '\173-\377' Z" converts all characters past 'z' to Z's |
| 46 | #+ and squeezes multiple consecutive Z's, |
| 47 | #+ which gets rid of all the weird characters that the previous |
| 48 | #+ translation failed to deal with. |
| 49 | # Finally, "tr Z ' '" converts all those Z's to whitespace, |
| 50 | #+ which will be seen as word separators in the loop below. |
| 51 | |
| 52 | # *********************************************************************** |
| 53 | # Note the technique of feeding/piping the output of 'tr' back to itself, |
| 54 | #+ but with different arguments and/or options on each successive pass. |
| 55 | # *********************************************************************** |
| 56 | |
| 57 | |
| 58 | for word in $wlist # Important: |
| 59 | # $wlist must not be quoted here. |
| 60 | # "$wlist" does not work. |
| 61 | # Why not? |
| 62 | do |
| 63 | strlen=${#word} # String length. |
| 64 | if [ "$strlen" -lt "$MINSTRLEN" ] # Skip over short strings. |
| 65 | then |
| 66 | continue |
| 67 | fi |
| 68 | |
| 69 | grep -Fw $word "$WORDFILE" # Match whole words only. |
| 70 | # ^^^ # "Fixed strings" and |
| 71 | #+ "whole words" options. |
| 72 | done |
| 73 | |
| 74 | exit $? |