gpt4 book ai didi

linux - 拼写检查器 shell 脚本

转载 作者:太空狗 更新时间:2023-10-29 11:07:57 25 4
gpt4 key购买 nike

我有一些问题。我遇到了一个脚本问题,该脚本应该是一个简单的拼写检查器。

它的意思是,当发现一个错误的词时,它会提示用户输入该词的正确拼写,如果用户输入正确的拼写,它就会显示更正后的词以及下面的单词不正确(在检查完所有单词后)。但是,如果用户只是按下 Enter,那么它会将这个单词作为正确的拼写并将其放入 ~./memory 中,这样如果再次运行,它就会忽略这个单词。

截至目前,单词的正确/错误拼写没有显示,~./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"

最佳答案

我编辑了您脚本的敏感部分,这是一个很好的猜测,有助于找出实际问题。我稍微结合了自己的逻辑,但没有太在意。我将编辑后的脚本附在这个问题上,希望它能帮助您找到问题,我建议您进一步阅读以下主题:

以上三个链接摘自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

关于linux - 拼写检查器 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35734849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com