Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!