这里是我的任务:逐行从文件中读取数据。对于每一行,如果它满足某些条件,则要求用户输入一些内容,并根据用户的输入进行下一步操作。
我知道如何从 shell 脚本中逐行读取内容:
我知道如何从 shell 脚本中逐行读取内容:
while read line; do
echo $line
done < file.txt
然而,如果我想在循环体内与用户进行交互怎么办。概念上,这是我想要的:
while read line; do
echo "Is this what you want: $line [Y]es/[n]o"
# Here is the problem:
# I want to read something from standard input here.
# However, inside the loop body, the standard input is redirected to file.txt
read INPUT
if [[ $INPUT == "Y" ]]; then
echo $line
fi
done < file.txt
我应该使用其他方法来读取文件吗?还是使用另一种方法读取stdin?