【问题标题】:Pause shell script until user presses enter暂停 shell 脚本,直到用户按下回车键
【发布时间】:2023-04-02 13:02:01
【问题描述】:

当我阅读文件时

示例脚本

while read file
do
temp = $(echo $file)
read -p "Press Enter to continue"
echo $temp
done < test.txt

我想暂停脚本直到我按下 ENTER

【问题讨论】:

  • 那么脚本的当前行为是什么?
  • 我不能暂停脚本它只是直接打印最后一行

标签: bash shell while-loop


【解决方案1】:

read 默认从标准输入读取,它被重定向到文件,所以它从文件中获取行。您可以重定向回终端:

read -p "Press Enter to continue" </dev/tty

另一种选择是使用不同的 FD 进行文件重定向

while read -u 3
do
    ...
done 3< test.txt

【讨论】:

  • 或者让交互式读取命令使用stdin,并将文件重定向放在不同的文件描述符上
猜你喜欢
  • 1970-01-01
  • 2014-03-28
  • 2017-07-03
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多