Bash 如何在一个“循环”中暂停;读第行“;

Bash 如何在一个“循环”中暂停;读第行“;,bash,Bash,我知道 (1) 我可以使用 while read line do something done < file 但是,暂停技巧在这样的读取行循环中不起作用 while read line do something read -p "press any key to continue..." ## suppose to pause here done < file 读取行时 做 某物 阅读-p“按任意键继续…”##假设在此处暂

我知道
(1) 我可以使用

while read line  
do  
    something  
done < file 
但是,暂停技巧在这样的读取行循环中不起作用

while read line  
do  
    something  
    read -p "press any key to continue..."  ## suppose to pause here  
done < file 
读取行时
做
某物
阅读-p“按任意键继续…”##假设在此处暂停
完成<文件

那么这是为什么?如何克服它呢?

您的read命令也从stdin(从您的文件)读取数据

对于Linux,您可以使用以下功能:

read -p "press any key to continue..." < /dev/tty
read-p“按任意键继续…”
您的read命令也从stdin(从您的文件)读取数据

对于Linux,您可以使用以下功能:

read -p "press any key to continue..." < /dev/tty
read-p“按任意键继续…”
exec 3
首先打开文件描述符3(shell通常不使用),然后将文件的内容发送给它

然后,
读取
从该文件描述符(
-u3
)读取,循环显示结果并等待用户对标准输入的输入


循环不起作用的原因是重定向了文件中两个
read
调用的输入。从循环中的标准输入读取的任何内容都将从文件中获取数据。

exec 3
首先打开文件描述符3(shell通常不使用),然后将文件的内容发送给它

然后,
读取
从该文件描述符(
-u3
)读取,循环显示结果并等待用户对标准输入的输入



循环不起作用的原因是重定向了文件中两个
read
调用的输入。从循环中的标准输入读取的任何内容都将从文件中获取数据。

这样做的一个缺点是现在无法使用
/script绕过暂停。另一个缺点是现在无法使用
/script绕过暂停
exec 3< words
while read -u 3 word; do
    printf 'Got "%s"\n' "$word"
    read -p 'Press enter.'
done