"Press enter" 在 bash 管道中

"Press enter" while in a bash pipe

OK,标题可能不是很清楚。我试图做一些非常简单的事情:逐行循环文件,并添加一个简单的 read 来等待用户每次按下回车键。但它从管道而不是标准输入中读取。我尝试使用一些重定向但没有找到正确的咒语:

sort -R words.list |  while read p; do speak "$p"; read; done

上面的方法不起作用的原因很明显。但是我可以在第二次阅读时做类似 read <&0read -u /dev/stdin 的事情吗?

使用read <&1

sort -R word.list | while read p; do speak "$p"; read <&1; done

Bash File Descriptors

3.6.8 Duplicating File Descriptors

The redirection operator

[n]<&word

is used to duplicate input file descriptors. If word expands to one or more digits, the file descriptor denoted by n is made to be a copy of that file descriptor. If the digits in word do not specify a file descriptor open for input, a redirection error occurs. If word evaluates to ‘-’, file descriptor n is closed. If n is not specified, the standard input (file descriptor 0) is used.

Order of redirections
In the shell, what does " 2>&1 " mean?

重复输入 fd 并使用副本。

# duplicate 10 fd as 0
exec 10<&0
sort -R word.list | while read p; do speak "$p"; read -u 10; done

您只能为这个命令重定向它:

{ sort -R word.list | while read p; do speak "$p"; read -u 10; done; } 10<&0