| 1 | #!/bin/bash |
| 2 | # agram2.sh |
| 3 | # Example of nested command substitution. |
| 4 | |
| 5 | # Uses "anagram" utility |
| 6 | #+ that is part of the author's "yawl" word list package. |
| 7 | # http://ibiblio.org/pub/Linux/libs/yawl-0.3.2.tar.gz |
| 8 | # http://bash.deta.in/yawl-0.3.2.tar.gz |
| 9 | |
| 10 | E_NOARGS=86 |
| 11 | E_BADARG=87 |
| 12 | MINLEN=7 |
| 13 | |
| 14 | if [ -z "$1" ] |
| 15 | then |
| 16 | echo "Usage $0 LETTERSET" |
| 17 | exit $E_NOARGS # Script needs a command-line argument. |
| 18 | elif [ ${#1} -lt $MINLEN ] |
| 19 | then |
| 20 | echo "Argument must have at least $MINLEN letters." |
| 21 | exit $E_BADARG |
| 22 | fi |
| 23 | |
| 24 | |
| 25 | |
| 26 | FILTER='.......' # Must have at least 7 letters. |
| 27 | # 1234567 |
| 28 | Anagrams=( $(echo $(anagram $1 | grep $FILTER) ) ) |
| 29 | # $( $( nested command sub. ) ) |
| 30 | # ( array assignment ) |
| 31 | |
| 32 | echo |
| 33 | echo "${#Anagrams[*]} 7+ letter anagrams found" |
| 34 | echo |
| 35 | echo ${Anagrams[0]} # First anagram. |
| 36 | echo ${Anagrams[1]} # Second anagram. |
| 37 | # Etc. |
| 38 | |
| 39 | # echo "${Anagrams[*]}" # To list all the anagrams in a single line . . . |
| 40 | |
| 41 | # Look ahead to the Arrays chapter for enlightenment on |
| 42 | #+ what's going on here. |
| 43 | |
| 44 | # See also the agram.sh script for an exercise in anagram finding. |
| 45 | |
| 46 | exit $? |