- 論壇徽章:
- 0
|
題目:
Write a script named nonwords that is passed a file containing one word per line, and checks to see if each word exists.
nonwords 輸出在“字典文件”沒有找到的單詞,一行一個.
Once again, accept input from a file or from standard input.
不區(qū)分大小寫;
Extend your nowords script to include a -i option that ignores any final “s” on each word (if there is one), and then checks to see if the word exists.
我的解法:
#!/bin/bash
if [[ $1 = "-i" ]]; then #判斷是否含有-i參數(shù)
while (read word )
do
l_char=$(echo $word | grep -i -o '.$') #取出最后一個字符
if [ $l_char = 's' ];then
n_word=$(echo $word | sed s/.$//) #如果是s就刪除
else
n_word=$word #否則直接賦值到新的變量中
fi
if ( ! grep -iqw $n_word $3) #不區(qū)分大小的情況下比較變量和字典文件,如果不存在就輸出
then
echo $n_word
fi
done < $2 #繼續(xù)讀取
else #如果沒有-i參數(shù),就直接進(jìn)行簡單步驟
while (read word)
do
if (! grep -iwq word $3)
then
echo $word
fi
done < $1
fi
我在自己電腦上中執(zhí)行語句是:
./nonword check.txt dict.txt
和
./nonword -i check.txt dict.txt
執(zhí)行沒有問題
但是在學(xué)校的在線系統(tǒng)中檢測會報錯:
./nonwords: line 26: $1: ambiguous redirect
...Your nonwords cannot handle words from stdin
...Your nonwords cannot handle words from a file
./nonwords: line 26: $2: ambiguous redirect
...Your nonwords cannot handle the -i option
雖能告訴我怎麼改正? |
|