csuriano23

csuriano23

Using elixir variable as System.cmd input

Hi there,
I was wondering if there is a way to redirect input to a system command from an elixir variable instead that from a file.

In other words I’d like to invoke a command like this:

wc -l < myfile

using Elixir’s System.cmd:

my_stream = """
a
b
c
"""

"wc"
|> System.find_executable
|> System.cmd(["-l", ...])

After some search I think that the only way could be using in some way IO.Stream, but I can’t figure out how.
The variable my_stream contains the stream to be provided to wc -l.

Any suggestion?

Thank you
Carlo

Most Liked

smathy

smathy

That < syntax in shell is just a way to pipe the contents of a file onto the STDIN of the command. Interestingly, wc will also take a filename as a regular argument. So for either of those to work you would need to write your string to a file and use that (although I’m not sure the < would work anyway, because I believe it’s a shell feature).

But, it’s fairly simple to write to the STDIN of a command from Elixir without using any special shell syntax, just that you need to use the underlying Port module directly (which is what System.cmd is using under the hood).

All that having been said, if you’re really just trying to count the number of lines in the string:

my_stream |> String.split("\n") |> Enum.count()
smathy

smathy

Nice, thanks for sharing that. Very cool.

csuriano23

csuriano23

Thank you.

My real purpose is not to count the line of a file, it was a simplification of what I am trying to, launching an external tool via System.cmd and providing an input from STDIN. I am going to give a look to the port module, thank you

Last Post!

csuriano23

csuriano23

It seems a really good solution, thank you

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement