拼写检查程序shell脚本

时间:2016-03-01 22:25:47

标签: linux bash shell

我有一些问题。我的脚本问题应该是一个简单的拼写检查程序。

它的意思是,当找到错误的单词时,它会提示用户输入单词的正确拼写,如果用户输入正确的拼写,它将随后显示更正的单词以及下面的错误单词(经过所有单词后)。但是,如果用户只是点击Enter,那么它将把这个单词作为正确的拼写并将其放在〜。/ memory中,这样如果再次运行,它将忽略这个单词。

截至目前,单词的正确/错误拼写未被显示,并且没有任何内容被记住"在〜。/记忆中。说实话,我不确定为什么。

#! /bin/bash
wrongWords=`aspell list < $1`
touch ~/.memory

for wrongWord in $wrongWords
    do
            echo  $wrongWord "is mispelled."
            read -p "Press ""Enter"" to keep this spelling, or type a correction here: " newWord
            if [ "$newWord" = "" ]
                    then
                            echo "$newWord" >> ~/.memory
            fi
    done

printf "%-20s %-20s \n Mispelled: Corrections:"
printf "\n $wrongWord $newWord"

1 个答案:

答案 0 :(得分:3)

我编辑了脚本的敏感部分,这是尝试查找实际问题的一个很好的猜测。我将自己的逻辑略微合并到了这里,但没有多少关心。我将编辑过的脚本附加到这个问题上,希望这是找到问题的一个很好的步骤,我建议你进一步阅读以下主题:

上面的三个链接取自this Bash Guide,这是从那里学习Bash的主要和唯一来源之一。

我还建议您在将来需要时使用ShellCheck检查脚本。

编辑过的脚本。不一定正确。

#! /bin/bash
# This script should be checked before use.
# It is not necessarily correct.

wrong_words=()
new_words=()

while read -r ww; do

    printf '%s is mispelled.\n' "$ww"

    wrong_words+=("$ww")

    read -rp "Press \"Enter\" to keep this spelling, or type a correction here: " nw

    # User provided a correction to $ww
    if [[ $nw ]]; then
        printf 'User corrected %s to %s\n' "$ww" "$nw"
        new_words+=("$nw")
    else
        printf 'User decided to keep the spelling of %s even though it was detected to be wrong.\n' "$ww"
    fi

done < <(aspell list < "$1")

# Saving new words to ~/.memory_words
printf '%s\n' "${new_words[@]}" >> ~/.memory_words

# Displaying info. Not necessarily useful.
printf 'New word: %s\n' "${new_words[@]}"
printf 'Wrong word: %s\n' "${wrong_words[@]}"

新单词会保存到new_words数组中。错误的单词将保存到wrong_words数组中。 在脚本末尾new_words数组附加到文件〜/ .memory_words