Bash 在流水线读取循环中读取键盘输入

Bash 在流水线读取循环中读取键盘输入,bash,unix,sh,Bash,Unix,Sh,在脚本中,我希望逐行读取流程输出,并获得用户的确认。到目前为止,我已经做到了: mycommand-outputpiped | while (read line) do read line #dostuff read confirm #oops -> this read the next item from the pipe, not the keyboard done 因此,我试图补充: read confirm < /dev/stdin 读取确认

在脚本中,我希望逐行读取流程输出,并获得用户的确认。到目前为止,我已经做到了:

mycommand-outputpiped | while (read line)
do
   read line
   #dostuff

   read confirm #oops -> this read the next item from the pipe, not the keyboard
done
因此,我试图补充:

read confirm < /dev/stdin
读取确认
但它并没有改变这件事,它仍然读取管道的下一行。。。
如何处理此问题?

两个
read
命令都从
while
循环继承的标准输入流中读取。以下几点应该有效;您的第二次读取需要直接从终端读取,而不是从
/dev/stdin
(即管道)读取

mycommand在读取行时通过管道输出
做
#做事
读取确认

请注意,在
while
条件中只有一个
read
,并且它不包含在括号中(这将创建子shell,
仅在该子shell中可用,而不在循环体中)。

不相关,但是,为什么在变量
的条件列表和循环体中都有一个
读取
命令?我不确定这一点-我相信第一个检查仍然有数据要读取,而第二个检查读取数据。我不知道如何清理它
mycommand-outputpiped | while read line
do
    # do stuff
    read confirm < /dev/tty
done