0

I have wrote a script which receives a user's sentence, i'd like to pipe the string to aspell and then get the INTERACTIVE screen's suggestions (in case user had some typos). Afterwards the words would be sent back to the script and I'd replace the typos with the corresponding words.

I'm aware of aspell options:

1) Check the file for typos and display a list of suggestions in interactive mode:

aspell check <file name>

2) Check the file for typos and display a list of suggestions not in interactive mode:

cat <filename> | aspell -a

but if I try option one, I get "Error: Stdin not a terminal.", I'll be glad to get any fruitful tip how to overcome this issue? :)

Thank you

  • 1
    For option 1, maybe creating a file descriptor for /dev/tty (which always is yr terminal) in a manner proposed by @John1024 at superuser.com/questions/834502/… .... or opt for the non-interactive mode, where you would get suggestions displayed, accept or modify each of them and then submit those corrections from within yr script.... – Cbhihe Aug 18 '18 at 8:37
  • @Cbhihe thank you for the assistance, I wrote: exec < /dev/tty it worked like a charm! :) – JammingThebBits Aug 18 '18 at 22:09
  • You should write a complete answer to your own question and then accept it, so others may benefit from it in the future, taking stock of the fact that your questions has an accepted answer. (In doing so you will not accrue karma points, but you will be doing a service to the community by enriching the database.) – Cbhihe Aug 19 '18 at 8:15
0

I think, you can't get away without using a temporary file. zsh has builtin support for that with its =(...) form of command substitution.

Here, to spell-check the content of a variable (of the output of printf %s $string):

{
  (){
    aspell check -x $1 >&3 3>&-
    cat $1
  } =(printf %s $string) | IFS= read -rd '' corrected_string
} 3>&1
0

Here is the solution, which worked for me:

TMP_FILE=`mktemp`

echo "${USER_INPUT}" > "${TMP_FILE}"

# Allows me to read user input below, assigns stdin to keyboard
exec < /dev/tty

# I'm assuming you have a aspell (spell checker) installed on your system
# invoke aspell and check for typos
/usr/bin/aspell check "${TMP_FILE}"

ANSWER=$(cat $TMP_FILE )

if [[ "${ANSWER}" != "${USER_INPUT}" ]]; then
    echo "After applying fixes: ${ANSWER}"
fi

Enjoy!

  • Note that using echo means input that contains backslashes (or things like -nene) will be mangled (hence the use of printf in my answer). Using command substitution means the tailing empty lines will be removed (hence the use of read in my answer). Note that without -x, aspell will leave a backup file behind. You'll also need to handle the clean-up of that tempfile (handled automatically by =(...)). – Stéphane Chazelas Aug 19 '18 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.