简体   繁体   中英

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.

I have already tried using the IO.gets/1 , but it reads just the first line of the output.

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. I'd expect Elixir to have some built-in solution, to end on EOF.

There is a IO.read/2 function I was looking for.

defmodule Mix.Tasks.Print do
  use Mix.Task

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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