在脚本中,我想逐行读取流程输出,并获得用户的确认。到目前为止,我已经做到了这一点:
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
但它并没有改变这一点,它仍然从管道中读取下一行...... 我该怎么办呢?
答案 0 :(得分:8)
两个read
命令都从继承自while
循环的标准输入流中读取。以下应该工作;您的第二次读取需要直接从终端读取,而不是/dev/stdin
(管道)。
mycommand-outputpiped | while read line
do
# do stuff
read confirm < /dev/tty
done
请注意,在read
条件中只有一个while
,并且不括在括号中(这将创建子shell,并{{1仅在子shell中可用,而不是循环体。