对于类,我需要编写一个 Bash 脚本,该脚本将从中获取输出ispell
,当我尝试在 while 循环内请求用户输入时,它只会将文件的下一行保存为用户输入。
我怎样才能在 while 循环中请求用户输入?
#!/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;
答案1
你不能在你的while
.你需要使用另一个文件描述符
尝试以下版本:
#!/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"
笔记
- 使用更多报价!它们至关重要。看http://mywiki.wooledge.org/Quotes和http://wiki.bash-hackers.org/syntax/words
bash
不是C
或Perl
,无需;
在两端各放线
答案2
#!/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