【问题标题】:cannot redirect stdin in bash无法在 bash 中重定向标准输入
【发布时间】:2019-12-30 08:55:15
【问题描述】:

我在玩文件描述符。一种常见的做法是使用文件描述符重定向 stdout 和 stderr,如下所示:

$ ls >files.txt 2>errors.txt

另一个例子可能是:

$ echo 'Hello there' > message.txt 2> errors.txt

但是以下方法不起作用:

$ echo 'Hello There' 0> input_message.txt > output_message.txt 2> errors.txt

似乎文件描述符 0 不起作用,因为 input_message.txt 为空,我希望找到短信 Hello There

这是为什么呢?

【问题讨论】:

  • 0stdin。你到底想做什么?
  • 我想在文件中输入文本(即 Hello there),但我想即时执行此操作,这就是我传递标准输入而不是文件的原因。
  • 请澄清您所说的即时是什么意思?你有例子吗?
  • @Harry Hello there 是一个参数,它通过execve 调用而不是标准输入传递给echo

标签: bash stdin file-descriptor


【解决方案1】:

您使用<(或0<)重定向标准输入,例如:

command <file

或:

command <&fd

其中fd 是打开以供读取的文件描述符。

对于您的示例,由于 echo 忽略标准输入,因此此更正不会改变行为。

Bash Reference Manual § 3.6 Redirections

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    相关资源
    最近更新 更多