Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
Spell checker shell script

I have some questions. I'm having an issue with a script that's supposed to be a simple spell checker.

What it is meant to do is, when a wrong word is found, it will prompt the user to enter the correct spelling of the word, if the user enters the correct spelling it will then then display the corrected word along with the incorrect word down below (after going through all the words). But, if the user just hits Enter, then it will take the word as a correct spelling and place it in ~./memory, so that if ran again, it will overlook this word.

As of right now, the correct/incorrect spellings of the words are not being displayed and nothing is being "remembered" in ~./memory. And to be honest, I'm not really sure why.

#! /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"
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I edited sensitive parts of your script, which are a good guess in trying to find the actual problem. I slightly combined my own logic into this, but without much care. I attach the edited script to this question with a hope that it will be a good step in finding your problem, and I suggest you to read further about the following subjects:

  • Do not read lines with for.
  • Process Substitution.
  • Test and Conditionals.

The three links above are taken from this Bash Guide which is one of the main, and only sources to learn Bash from out there.

I would like to also recommend you to use ShellCheck to check your scripts when needed in the future.


Edited script. Not necessarily correct.

#! /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 are saved to new_words array. Wrong words are saved to wrong_words array. In the end of the script new_words array is appended to the file ~/.memory_words.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda