Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spell checker shell script

Tags:

linux

bash

shell

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"
like image 826
kenny10009 Avatar asked Jul 17 '26 22:07

kenny10009


1 Answers

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.

like image 200
Rany Albeg Wein Avatar answered Jul 20 '26 15:07

Rany Albeg Wein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!