我正在尝试制作一个简单的用户插入的单词词典,这些单词收集在 txt 文件中,然后是学习它们的函数。两种不同语言的单词由选定的分隔符分隔,然后检查用户输入的答案是否正确。我遇到了第一次迭代后 txt 文件行被跳过的问题,因此我使用了一个子 shell,如果我没有计算正确/错误答案的变量,它就可以正常工作。我真的不知道如何解决这个问题。我的一段代码:
count=0
ans=0
while IFS= read -r line; do
(...)
( # without subshell the lines from txt file were skipped
exec 0< /dev/tty # I used this command to prevent skipping the "read check" line
# as it happened before
read check
if [ "$check" == "$correct" ];then
echo "Correct"
let ans++
let count++
else
echo "Wrong"
let count++
fi
)
done < "file.txt"
echo "Answered $ans out of $count words correctly"