| 1 | #!/bin/bash |
| 2 | # lookup: Does a dictionary lookup on each word in a data file. |
| 3 | |
| 4 | file=words.data # Data file from which to read words to test. |
| 5 | |
| 6 | echo |
| 7 | echo "Testing file $file" |
| 8 | echo |
| 9 | |
| 10 | while [ "$word" != end ] # Last word in data file. |
| 11 | do # ^^^ |
| 12 | read word # From data file, because of redirection at end of loop. |
| 13 | look $word > /dev/null # Don't want to display lines in dictionary file. |
| 14 | # Searches for words in the file /usr/share/dict/words |
| 15 | #+ (usually a link to linux.words). |
| 16 | lookup=$? # Exit status of 'look' command. |
| 17 | |
| 18 | if [ "$lookup" -eq 0 ] |
| 19 | then |
| 20 | echo "\"$word\" is valid." |
| 21 | else |
| 22 | echo "\"$word\" is invalid." |
| 23 | fi |
| 24 | |
| 25 | done <"$file" # Redirects stdin to $file, so "reads" come from there. |
| 26 | |
| 27 | echo |
| 28 | |
| 29 | exit 0 |
| 30 | |
| 31 | # ---------------------------------------------------------------- |
| 32 | # Code below line will not execute because of "exit" command above. |
| 33 | |
| 34 | |
| 35 | # Stephane Chazelas proposes the following, more concise alternative: |
| 36 | |
| 37 | while read word && [[ $word != end ]] |
| 38 | do if look "$word" > /dev/null |
| 39 | then echo "\"$word\" is valid." |
| 40 | else echo "\"$word\" is invalid." |
| 41 | fi |
| 42 | done <"$file" |
| 43 | |
| 44 | exit 0 |