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

Bash 在Shell脚本中的循环中获取用户输入,bash,sh,Bash,Sh,我试图在shell脚本的while循环中获取用户输入 ls| while read p do echo $p read key done 我无法读取来自stdin(键盘)的输入,因为我的输入是通过ls管道传输的。 有人能帮我在循环中获得用户输入吗 您可以使用文件描述符0从stdin读取。或者只使用/dev/tty。其中,$是当前进程的pid ls| while read p do echo $p read key < /proc/$$/fd/0 # OR

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

ls| while read p
do
   echo $p

   read key
done
我无法
读取
来自
stdin
(键盘)的输入,因为我的输入是通过
ls
管道传输的。
有人能帮我在循环中获得用户输入吗

您可以使用
文件描述符0
从stdin读取。或者只使用
/dev/tty
。其中,
$
是当前进程的pid

ls| while read p
do
   echo $p
   read key < /proc/$$/fd/0
   # OR read key < /dev/tty
done
ls |读取p时
做
回声$p
读取键
您可以使用
文件描述符0
从stdin读取数据。或者只使用
/dev/tty
。其中,
$
是当前进程的pid

ls| while read p
do
   echo $p
   read key < /proc/$$/fd/0
   # OR read key < /dev/tty
done
ls |读取p时
做
回声$p
读取键
为什么要用管道将ls传输到read命令?使用
read key@PS。它起作用了。同样的问题也在你的链接中得到了解决。我将删除这个问题。@Fawzan:请看,@Inian读得很好+1作为提示。为什么要将ls传输到read命令?使用
read key@PS。它起作用了。同样的问题也在你的链接中得到了解决。我将删除这个问题。@Fawzan:请看,@Inian读得很好+小费1英镑。