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

45 lines
1.1 KiB
Bash
Raw Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
2001-09-04 13:27:31 +00:00
# lookup: Does a dictionary lookup on each word in a data file.
2001-07-10 14:25:50 +00:00
file=words.data # Data file from which to read words to test.
2012-04-04 22:51:18 +00:00
echo
echo "Testing file $file"
2001-07-10 14:25:50 +00:00
echo
while [ "$word" != end ] # Last word in data file.
2006-12-20 21:11:55 +00:00
do # ^^^
2001-09-04 13:27:31 +00:00
read word # From data file, because of redirection at end of loop.
2001-07-10 14:25:50 +00:00
look $word > /dev/null # Don't want to display lines in dictionary file.
2012-04-04 22:51:18 +00:00
# Searches for words in the file /usr/share/dict/words
#+ (usually a link to linux.words).
2001-09-04 13:27:31 +00:00
lookup=$? # Exit status of 'look' command.
2001-07-10 14:25:50 +00:00
if [ "$lookup" -eq 0 ]
then
echo "\"$word\" is valid."
else
echo "\"$word\" is invalid."
fi
done <"$file" # Redirects stdin to $file, so "reads" come from there.
2001-07-10 14:25:50 +00:00
echo
exit 0
2001-09-04 13:27:31 +00:00
# ----------------------------------------------------------------
# Code below line will not execute because of "exit" command above.
2001-07-10 14:25:50 +00:00
# Stephane Chazelas proposes the following, more concise alternative:
while read word && [[ $word != end ]]
2001-07-10 14:25:50 +00:00
do if look "$word" > /dev/null
then echo "\"$word\" is valid."
else echo "\"$word\" is invalid."
fi
done <"$file"
2001-07-10 14:25:50 +00:00
exit 0