【问题标题】:Linux: How would I pipe text into a program properly?Linux:如何正确地将文本通过管道传输到程序中?
【发布时间】:2011-01-14 00:48:09
【问题描述】:

我看过但找不到任何东西。例如,我使用的一个 TTS 程序可以让您执行以下操作:

~#festival -tts | echo "I am to be spoken"

这真的很好,但是对于我使用的程序,例如 hexdump,我不知道如何将文本输入其中。我真的可以使用其中的一些东西,我尝试过(但失败)的一些例子是这样的:

~#gtextpad < dmesg
      //(how do I put the contents into the text pad for display? not just into a file)
~#hexdump | echo "I am to be put into hexdump"
      //(How do I get hexdump to read the echo? It normally reads a file such as foo.txt..)

【问题讨论】:

    标签: linux file terminal command pipe


    【解决方案1】:

    来自hexdump(1) 手册页:

    hexdump 实用程序是一个过滤器,它以用户指定的格式显示指定的文件或标准输入(如果未指定文件)。

    所以:

    echo "I am to be put into hexdump" | hexdump
    

    【讨论】:

      【解决方案2】:

      管道中的数据流(由管道符号分隔的一系列命令)从左到右流动。因此,下面 command1 的输出会转到 command2 的输入,以此类推。

      command1 |
      command2 |
      command3 arg1 arg2 arg3 |
      sort |
      more
      

      因此,要将“echo”的输出转换为“hexdump”,请使用:

      echo "I am to be dumped" | hexdump
      

      我看不出你展示的“节日”命令是如何工作的。 shell 做了足够多的管道工作,没有做出无根据的假设和做很多诡计,然后仍然依赖内核中的调度决策来让事情“正确”,我不明白它是如何工作的。

      【讨论】:

        【解决方案3】:

        这里有一些将文本传递到 hexdump 的方法

        标准输入:

        echo "text" | hexdump

        这里是文档

        hexdump <<EOF
        text
        EOF
        

        处理整个文件

        hexdump file
        

        【讨论】:

        • 啊。反过来说,现在我可以做一些我想做的有用的自动化事情了。
        【解决方案4】:

        在 bash 上,你也可以这样做

        hexdump <<< "Pipe this text into hexdump"
        

        【讨论】:

        猜你喜欢
        • 2017-03-22
        • 2015-10-05
        • 1970-01-01
        • 2014-02-03
        • 1970-01-01
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多