首页 > 解决方案 > read -t 没有在 bash 中暂停我的 while 循环

问题描述

我试图在 bash 中创建一个简单的循环,一个一个地列出目录中的所有文件。

我想Enter在进行下一个之前必须先打。我认为添加read会使循环暂停。但它并没有暂停。它一次列出所有文件。这是我正在使用的命令

find . -type f | while read -r line; do echo "$line"; read -t 1; done`

标签: bashfor-loopfind

解决方案


你可以这样做:

# redirect fd=3 from 0
exec 3<&0

while IFS= read -rd '' line; do
   echo "$line"
   # read from fd=3 and wait for 1 sec
   read -u 3 -t 1
done < <(find . -type f -print0)

# close fd=3
exec 3<&-

推荐阅读