#!/bin/bash # # Look up anagrams # PG=alu WL=/usr/dict/word.list LIM1=4 # If this many or fewer matches, list eash on it's own line. LIM2=79 # Maximum number of characters on a line. LIM3=23 # If more than this many lines, output to 'less'. TMPFILE1=/tmp/${PG}-tmp1_.tmp TMPFILE2=/tmp/${PG}-tmp2_.tmp function display_help() { echo "$PG: Given a string of letter such as \"tastier\" (without the quotes)," echo "$PG: find SINGLE WORD anagrams. Invoke 'alu' with no argument to start" echo "$PG: in interactive mode, then enter a completely empty line to exit." } if [ -z "$1" ] ; then ONESHOT="0" display_help else ONESHOT="1" fi while [ -n "$ONESHOT" ] ; do if [ "$ONESHOT" == "0" ] ; then read -e -p "$PG>" WORD1 WORD2 if [ -z "$WORD1" ] ; then exit 0 fi elif [ "$ONESHOT" == "1" ] ; then WORD1="$1" WORD2="$2" unset ONESHOT fi if [ -n "$WORD2" ] ; then display_help else # Check the user's argument and output a list of anagrams. gawk -v arg=$WORD1 ' \ BEGIN { ok = 0; arg_len = length(arg); if (arg_len < 2) exit 2; s = ""; for (i=1;i<=arg_len;i++) { c = tolower(substr(arg,i,1)); n = index("abcdefghijklmnopqrstuvwxyz",c); if (n > 0) s = s c; else exit 2; } ok = 1; cnt = 0; } { len = length($1); if (len != arg_len) next; t = $1; for (i=1;i<=arg_len;i++) { c = substr(s,i,1); n = index(t,c); if (n == 0) next; t = substr(t,1,n-1) "-" substr(t,n+1,len-n); } if ($1 == s) next; print $1 cnt++; } END { if (ok != 1) exit 2; if (cnt < 1) exit 1; exit 0; } ' $WL RTN=$? if [ $RTN -eq 2 ] ; then display_help elif [ $RTN -eq 1 ] ; then echo "No anagrams found." fi fi done