【发布时间】:2014-01-04 17:59:25
【问题描述】:
我正在 unix 中编写一个名为 pl_dict 的 TC shell 脚本,它接受单数形式的英文单词列表作为输入,并在单独的行中打印出每个单词的复数形式。它使用一个包含英语单词列表的文件和另一个接受单数形式的英语单词作为参数的 c 程序,并打印该单词的复数形式。 这是我的代码:
set dictionary = (/usr/share/dict/words)
set irregular = (/share/files/irregular.txt)
#go over all the input words
foreach word ($argv[*])
set irregularWord = `grep $word $irregular | cut -d" " -f1`
#the word is found in irregular.txt file
if ("$irregularWord" != "") then
gcc -o pluralize pluralize.c
./pluralize -f irregular.txt $word
else #the word is not found in the irregular file
#search for it in the dictionary
set realEnglishWord = `grep $word $dictionary`
#the word is a real English word
if ("$realEnglishWord" != "") then
gcc -o pluralize pluralize.c
./pluralize $word
else
echo "$word":" word not found in dictionary."
endif
endif
end
在我尝试运行它之前它工作得很好:pl_dict fish foot fox house mouse
我得到的输出是:
fish
feet
foox: word not found in dictionary.
Word too long.
问题是什么,我该如何解决?
谢谢。
【问题讨论】:
-
尝试一些调试回显以查看 $word 是什么... grep 可能匹配字典中的几行... 如果是这样,您可以使用 awk 并进行 $1 的精确匹配