在循环内读取用户输入 [英] Read user input inside a loop

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

问题描述

我有一个类似于以下内容的 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 重定向,它试图从文件 filename 中读取.

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.

还有其他方法吗?

推荐答案

从控制终端设备读取:

read input </dev/tty

更多信息:http://compgroups.net/comp.unix.shell/Fixing-stdin-inside-a-redirected-loop

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆