在 bash 中重定向 STDIN?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3247517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect STDIN in bash?
提问by wRAR
For an example i would like to login to mysql with a password. i KNOW i can use -pmypass but i want to learn how do i redirect stdin in bash. So my test is
例如,我想使用密码登录 mysql。我知道我可以使用 -pmypass 但我想学习如何在 bash 中重定向 stdin。所以我的测试是
mysql -u limited_user -p <text to redirect into stdin when it prompts for my pass>
I seen < filename before but i dont want to store the data in a file. I tried & and &- but had no luck. I am not sure what &- does.
我之前看过 < filename 但我不想将数据存储在文件中。我试过 & 和 &- 但没有运气。我不确定 &- 是做什么的。
采纳答案by Matthew Flaschen
Normally, it's just:
通常,它只是:
echo password|command
But many programs, including MySQL, that read passwords don't actually read from stdin. Rather, they interact with the terminal directly. See Trick an application into thinking its stdin is interactive, not a pipe.
但是许多读取密码的程序,包括 MySQL,实际上并不是从 stdin 读取的。相反,它们直接与终端交互。请参阅欺骗应用程序使其认为其标准输入是交互式的,而不是管道。
回答by wRAR
command <<< "input"
That's a bashism and echosolution is more common in scripts, though my solution can be more handy in an interactive shell (and I doubt any of these solutions works with mysql, but your question is more general).
这是一种bashism,echo解决方案在脚本中更常见,尽管我的解决方案在交互式shell中可能更方便(我怀疑这些解决方案中的任何一个都适用于mysql,但您的问题更普遍)。

