在Shell脚本中的循环内获取用户输入

时间:2016-09-28 06:00:58

标签: bash sh

我试图在shell脚本中的while循环中获取用户输入。

ls| while read p
do
   echo $p

   read key
done

我无法read来自stdin(键盘)的输入,因为我的输入是通过ls传输的。 有人可以帮我在循环中获取用户输入吗?

1 个答案:

答案 0 :(得分:2)

您可以使用file descriptor 0从标准输入读取。或者只使用/dev/tty$$是您当前流程的pid的位置。

ls| while read p
do
   echo $p
   read key < /proc/$$/fd/0
   # OR read key < /dev/tty
done
相关问题