read -t 没有在 bash 中暂停我的 while 循环
read -t is not pausing my while loop in bash
我正在尝试在 bash 中创建一个简单的循环,一个一个地列出目录中的所有文件。
我想在继续下一个之前必须点击 Enter
。我认为添加 read
会使循环暂停。但它并没有暂停。它一次列出所有文件。这是我正在使用的命令
find . -type f | while read -r line; do echo "$line"; read -t 1; done`
I want to have to hit Enter before proceeding to the next one. I thought that adding read would make the loop pause
第一次 阅读 尝试 ProcSub and use a different FD。请注意,内部 read
就在 do
.
之后
while read -ru9 line; do read -p "$line"; done 9< <(find . -type f)
read 中的 -u
标志意味着它将使用给定的文件描述符,在这种情况下 9
如果您的 bash
足够新,您可以只使用 varname
作为 FD
类似
while read -ru "$fd" line; do read -p "$line"; done {fd}< <(find . -type f)
而不是硬编码 FD 的固定值。其中 "$fd"
只是任意 name/variable.
另请参阅本地终端中的帮助阅读。
你可以这样做:
# 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<&-
扩展@anubhava 的回答,在何处使用其他文件描述符并不重要。您可以使用它来读取来自 find
进程的输入,并获取用户输入以提示 stdin
(fd0) 上的下一个文件。关键是您必须使用两个不同的描述符,一个用于从 find
输入文件名,另一个用于从用户的击键中获取输入。例如,您还可以这样做:
while read -u 4 -r line; do
echo "$line"
read -n1 -t2 c
done 4< <(find . -type f)
它在功能上是等价的,除此之外,进程替换与find
的结果在fd4上重定向,而用户可以在[=12上提供输入=] 或 2 秒超时后,将自动显示下一个文件。
我正在尝试在 bash 中创建一个简单的循环,一个一个地列出目录中的所有文件。
我想在继续下一个之前必须点击 Enter
。我认为添加 read
会使循环暂停。但它并没有暂停。它一次列出所有文件。这是我正在使用的命令
find . -type f | while read -r line; do echo "$line"; read -t 1; done`
I want to have to hit Enter before proceeding to the next one. I thought that adding read would make the loop pause
第一次 阅读 尝试 ProcSub and use a different FD。请注意,内部 read
就在 do
.
while read -ru9 line; do read -p "$line"; done 9< <(find . -type f)
read 中的 -u
标志意味着它将使用给定的文件描述符,在这种情况下 9
如果您的 bash
足够新,您可以只使用 varname
作为 FD
类似
while read -ru "$fd" line; do read -p "$line"; done {fd}< <(find . -type f)
而不是硬编码 FD 的固定值。其中 "$fd"
只是任意 name/variable.
另请参阅本地终端中的帮助阅读。
你可以这样做:
# 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<&-
扩展@anubhava 的回答,在何处使用其他文件描述符并不重要。您可以使用它来读取来自 find
进程的输入,并获取用户输入以提示 stdin
(fd0) 上的下一个文件。关键是您必须使用两个不同的描述符,一个用于从 find
输入文件名,另一个用于从用户的击键中获取输入。例如,您还可以这样做:
while read -u 4 -r line; do
echo "$line"
read -n1 -t2 c
done 4< <(find . -type f)
它在功能上是等价的,除此之外,进程替换与find
的结果在fd4上重定向,而用户可以在[=12上提供输入=] 或 2 秒超时后,将自动显示下一个文件。