简体   繁体   English

如何将一个命令的输出通过unix管道传递到Elixir的Mix Task中?

[英]How to unix pipe the output of one command into Elixir's Mix Task?

I want to "pipe" the output of the cat command into an Elixir Mix Task and hold it in a variable as a binary. 我想将cat命令的输出“传递”到Elixir混合任务中,并将其作为二进制文件保存在变量中。

I have already tried using the IO.gets/1 , but it reads just the first line of the output. 我已经尝试使用IO.gets/1 ,但是它只读取输出的第一行。

cat textfile.txt | mix print
defmodule Mix.Tasks.Print do
  use Mix.Task

  def run(_argv) do
    Task.async(fn -> IO.gets("") end)
    |> Task.await(t)
    |> IO.puts() # prints the first line
  end
end

I want to get hold of the whole file's contents in a binary variable in Elixir and print it to the console, but I get just the first line. 我想在Elixir中的二进制变量中保存整个文件的内容,并将其打印到控制台,但是我只有第一行。 I'd expect Elixir to have some built-in solution, to end on EOF. 我希望Elixir有一些内置的解决方案,以EOF结尾。

There is a IO.read/2 function I was looking for. 我正在寻找一个IO.read/2函数。

defmodule Mix.Tasks.Print do
  use Mix.Task

  def run(_argv) do
    IO.read(:all)
    |> IO.puts() # prints all lines
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM