10

For class I need to write a Bash script that will take the output from ispell and when I try and request user input inside the while loop it just saves the next line of the file as the user input.

How could I go about requesting user input in the while loop?

#!/bin/bash
#Returns the misspelled words
#ispell -l < file

#define vars
ISPELL_OUTPUT_FILE="output.tmp";
INPUT_FILE=$1


ispell -l < $INPUT_FILE > $ISPELL_OUTPUT_FILE;

#echo a new line for give space between command
#and the output generated
echo "";

while read line;
do
   echo "'$line' is misspelled. Press "Enter" to keep";
   read -p "this spelling, or type a correction here: " USER_INPUT;

   if [ $USER_INPUT != "" ]
   then
      echo "INPUT: $USER_INPUT";
   fi


   echo ""; #echo a new line
done < $ISPELL_OUTPUT_FILE;

rm $ISPELL_OUTPUT_FILE;
mtk
  • 26,802
  • 35
  • 91
  • 130
Steven10172
  • 203
  • 2
  • 5

2 Answers2

9

You can't do that in your while. You need to use another file descriptor

Try the following version :

#!/bin/bash
#Returns the misspelled words
#ispell -l < file

#define vars
ISPELL_OUTPUT_FILE="output.tmp";
INPUT_FILE=$1


ispell -l < $INPUT_FILE > $ISPELL_OUTPUT_FILE;

#echo a new line for give space between command
#and the output generated
echo "";

while read -r -u9 line;
do
   echo "'$line' is misspelled. Press "Enter" to keep";
   read -p "this spelling, or type a correction here: " USER_INPUT;

   if [ "$USER_INPUT" != "" ]
   then
      echo "INPUT: $USER_INPUT";
   fi


   echo ""; #echo a new line
done 9< $ISPELL_OUTPUT_FILE;

rm "$ISPELL_OUTPUT_FILE"

See How to keep other commands from "eating" the input

NOTES

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82
0
#!/bin/bash

exec 3<> /dev/stdin

ispell -l < $1 | while read line
do
    echo "'$line' is misspelled. Press 'Enter' to keep"
    read -u 3 -p "this spelling, or type a correction here: " USER_INPUT

    [ "$USER_INPUT" != "" ] && echo "INPUT: $USER_INPUT"
done
k.parnell
  • 296
  • 1
  • 5