本文介绍了在循环中读取用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,类似于以下内容,

I am having a bash script which is something like following,

cat filename | while read line
do
    read input;
    echo $input;
done

但这显然不能提供正确的输出,因为当我在while循环中读取时,由于可能的I/O重定向,它试图从文件文件名读取.

but this is clearly not giving me the right output as when I do read in the while loop it tries to read from the file filename because of the possible I/O redirection.

还有其他方法可以做到吗?

Any other way of doing the same?

推荐答案

从控制终端设备读取:

read input </dev/tty

更多信息: http://compgroups.net/comp.unix. Shell/Fixing-stdin-inside-a-redirected循环

这篇关于在循环中读取用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:18
查看更多