LDP/LDP/guide/docbook/abs-guide/agram2.sh

47 lines
1.1 KiB
Bash
Raw Normal View History

2005-06-05 14:40:27 +00:00
#!/bin/bash
# agram2.sh
# Example of nested command substitution.
# Uses "anagram" utility
#+ that is part of the author's "yawl" word list package.
# http://ibiblio.org/pub/Linux/libs/yawl-0.3.2.tar.gz
2012-04-04 22:51:18 +00:00
# http://bash.deta.in/yawl-0.3.2.tar.gz
2005-06-05 14:40:27 +00:00
2012-04-04 22:51:18 +00:00
E_NOARGS=86
E_BADARG=87
2005-06-05 14:40:27 +00:00
MINLEN=7
if [ -z "$1" ]
then
echo "Usage $0 LETTERSET"
exit $E_NOARGS # Script needs a command-line argument.
elif [ ${#1} -lt $MINLEN ]
then
echo "Argument must have at least $MINLEN letters."
exit $E_BADARG
fi
FILTER='.......' # Must have at least 7 letters.
# 1234567
Anagrams=( $(echo $(anagram $1 | grep $FILTER) ) )
2006-12-20 21:11:55 +00:00
# $( $( nested command sub. ) )
2005-06-05 14:40:27 +00:00
# ( array assignment )
echo
echo "${#Anagrams[*]} 7+ letter anagrams found"
echo
echo ${Anagrams[0]} # First anagram.
echo ${Anagrams[1]} # Second anagram.
# Etc.
# echo "${Anagrams[*]}" # To list all the anagrams in a single line . . .
2012-04-04 22:51:18 +00:00
# Look ahead to the Arrays chapter for enlightenment on
2005-06-05 14:40:27 +00:00
#+ what's going on here.
2012-04-04 22:51:18 +00:00
# See also the agram.sh script for an exercise in anagram finding.
2005-06-05 14:40:27 +00:00
exit $?