2016-09-28 162 views
0

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

ls| while read p 
do 
    echo $p 

    read key 
done 

我不能readstdin(键盘)输入,因为我的输入是通过ls管道。 有人可以帮助我在循环内获得用户输入吗?

+0

你为什么要管ls到读命令? – Ujjwal

+0

使用'read key

+0

@PS。有效。同样的问题也会在您的链接中解决。我将删除该问题。 – Fawzan

回答

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