Shell Sript - Spell Checker Assign

Hi Folks. I am currently working on a script that has to spell check a file and print the output to the screen in 2 columns like this.

INCORRECT CORRECTION
whio who
weahter weather

The file will allow the user to override the ispell command and save any 'incorrect' words as 'correct' into a memory file which would then be used in any other runs of the script (as a dictionary file).

The code i currently have has a problem with it... and i cant figure it out for the life of me. Here is the code/script that i have written so far.

#!/bin/sh
# script that will spell check a file

#if user tries to quit, will say so, then quit on second attempt
trap "echo You are trying to exit; exit" 2


array=`ispell -l -p $HOME/memory.txt < $1`

correct=()
incorrect=""
let counter=1

for i in $array; do
        read -p "$i is mispelled. Press "Enter" to keep spelling, or type a correction here: " correction
        if [[$correction=""]] ; then
                echo $i >> $HOME/memory.txt
        else
                $correct[$counter]=$correction
        fi
let counter+=1
done

#prints the information
echo "CORRECTION       INCORRECT"
let count=1
for i in $array; do
        echo ${correct[$count]}        $i
let count+=1
done

For this code i get this output while runing:

sh-3.1$ ./wordspell testfile
hellio is mispelled. Press Enter to keep spelling, or type a correction here: hello
./wordspell: line 14: [[hello=]]: command not found
./wordspell: line 17: [1]=hello: command not found
whio is mispelled. Press Enter to keep spelling, or type a correction here: who
./wordspell: line 14: [[who=]]: command not found
./wordspell: line 17: [2]=who: command not found
wheter is mispelled. Press Enter to keep spelling, or type a correction here: weather
./wordspell: line 14: [[weather=]]: command not found
./wordspell: line 17: [3]=weather: command not found
CORRECTION INCORRECT
hellio
whio
wheter

I was wondering how i could sort out the line 14/17 errors. I have thought it would be something like missing () or {} in the array. But still no luck. Any ideas? Also, as you can see i want to print the correction, and the incorrect word (i have 2 arrays set, correct and incorrect which should contain the words for each). How would i get them to print out in columns like i have shown? Once i have this, i have another couple of things to code which im unsure of, but i will try after i get thsi first obstacle out of the way.

Thanks in advance

        if [[ "$correction" = "" ]] ; then
                echo $i >> $HOME/memory.txt
        else
                correct[$counter]=$correction
        fi

Jean-Pierre.

I attempted what you have given me, and it just gives me the exact same error messages (plus .... you aren't supposed to have spaces in your ##### = #### parts should be ####=#### right?)

Hope some advice still comes in, thanks for your time!

You execute your script with /bin/sh (line #1) which doesn't reconize the [[ ... ]] form of test.
You must use bash or ksh or replace [[ ... ]] by [ ... ]

There is no problem with spaces around the = operator.

Jean-Pierre.

Thank you Jean-Pierre for your persistence in regard to my problem. It is very much appreciated. My problem still stands, even with changing the #! line to /bin/bash .

I still get this:

sh-3.1$ ./wordspell testfile
hellio is mispelled. Press Enter to keep spelling, or type a correction here: hello
./wordspell: line 14: [[hello=]]: command not found
./wordspell: line 17: [1]=hello: command not found

Its got something to do with the arrays for sure - i have no idea why tho.

I might be thinking arrays too much like C++ arrays if that helps. lol. Anything is worth a look right?

evaluates to

and that is not a valid test. Try

instead,just to avoid problems with comparisons without something to compare with.

/Lakris

#!/bin/bash
# script that will spell check a file

#if user tries to quit, will say so, then quit on second attempt
trap "echo You are trying to exit; exit" 2


array=(`ispell -l -p $HOME/memory.txt < $1`)

correct=()
incorrect=""
let counter=1

for i in ${array[@]}; do
        read -p "$i is mispelled. Press "Enter" to keep spelling, or type a correction here: " correction
        if [[ "$correction" = "" ]] ; then
                echo $i >> $HOME/memory.txt
        else
                correct[$counter]=$correction
        fi
let counter+=1
done

#prints the information
echo "CORRECTION       INCORRECT"
let count=1
for i in ${array[@]}; do
        echo ${correct[$count]}        $i
let count+=1
done

Jean-Pierre.

Okay - thanks for your help Pierre. I edited the code as to how you recommended and it works almost perfectly now. My only problem is this:

sh-3.1$ ./wordspell2 testfile
hellio is mispelled. Press Enter to keep spelling, or type a correction here: hello
whio is mispelled. Press Enter to keep spelling, or type a correction here: who
wheter is mispelled. Press Enter to keep spelling, or type a correction here: weather

CORRECTION INCORRECT
hello hellio
whio
wheter

for whatever reason my code only prints out the first correct word, the incorrect word, then the rest is just the incorrect word without the correct work next to it. Here is the script all over again - anything jumping out? Thanks!

#!/bin/bash
# script that will spell check a file

trap "echo You are trying to exit; exit" 2

array=`ispell -l -p "$HOME/memory.txt" < $1`

correct=()
incorrect=""
let counter=1
for i in ${array[@]}; do
        read -p "$i is mispelled. Press "Enter" to keep spelling, or type a correction here: " correction
        if [[ "$correction" = "" ]] ; then
                echo $i >> "$HOME/memory.txt"
        else
                correct[$counter]=$correction
                echo ${correct[$counter]}
        fi
counter+=1
done

echo "CORRECTION       INCORRECT"
let count=1
for i in ${array[@]}; do
        echo ${correct[$count]}              $i
        let count+=1
done

I figured it out ... i was using let count+=1 instead of just count+=1.,..stupid mistake...

Now comes the hard parts....

Below is a run of my script (run through twice - once to spell check all the words in a file - then rerun to use the memory.txt file)

sh-3.1$ ./wordspell2 testfile
hellio is mispelled. Press Enter to keep spelling, or type a correction here: hello
whio is mispelled. Press Enter to keep spelling, or type a correction here: who
ege is mispelled. Press Enter to keep spelling, or type a correction here:
wheter is mispelled. Press Enter to keep spelling, or type a correction here: weather
1correct[*]
CORRECTION INCORRECT
hello hellio
who whio
ege ************************
weather wheter

The ege line should NOT print - this word is actually the name of my professor and therefore should be saved into the memory.txt file (which it does - but then NOT printed as part of the CORRECTION INCORRECT section).

Im assuming because of the use of the $array in the last part is the cause of this. Is there any way to get around this? Maybe use a second array and when the user enters a correction?

Any ideas? Im completely confused now - getting there though just need another little push!

Script as of now:


#!/bin/sh
# script that will spell check a file


trap "echo You are trying to exit; exit" 2

array=`ispell -l -p "$HOME/memory.txt" < $1`

correct=()
incorrect=""
let counter=1
for i in ${array[@]}; do
        read -p "$i is mispelled. Press "Enter" to keep spelling, or type a correction here: " correction
        if [[ "$correction" = "" ]] ; then
                echo $i >> "$HOME/memory.txt"
        else
                correct[$counter]=$correction
                #echo ${correct[counter]}
        fi
counter+=1
done

echo "CORRECTION       INCORRECT"
let count=1
for i in ${array[@]}; do
        echo ${correct[$count]}              $i
        count+=1
done

I think... the line ege is actually

but since $correct is empty for that index it in essence "becomes"

and leading spaces don't print.
Maybe You should have a check for emptyness like You did with the input? And only echo if $i is not empty.

/Lakris