這是我的任務:從文件中逐行讀取一些數據。對於每一行,如果滿足某些條件,則要求用戶輸入內容並根據用戶的輸入進行操作。如何在bash中讀取文件*和* stdin
我知道如何從一個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?
可能重複的[讀出的讀出循環內標準輸入的bash](http://stackoverflow.com/questions/8886683/read-stdin-bash-within-a-read-loop) – BroSlow