HaydenAscot

HaydenAscot

Error when using System.cmd

Hi there, I’m having some trouble with the “System” module, specifically the cmd function. My code is the following, which is exactly the same as the documentation, StackOverflow/Elixir forum answers, etc:

System.cmd("echo", ["Hello"])

I’m getting the following error:

** (ErlangError) Erlang error: :enoent
    (elixir 1.10.4) lib/system.ex:795: System.cmd("echo", ["Hello"], [])
    (elixir 1.10.4) lib/code.ex:926: Code.require_file/2

I understand the “enoent” means “No such file or directory”, however, I don’t understand what file or directory could possibly be required in this case. It’s probably an elementary mistake which wouldn’t surprise me as I’m new to Elixir/Erlang. There are 2 things worth mentioning, in my opinion. The first is that surrounding the words “echo” and “Hello” in single quotes instead of double quotes, so System.cmd(‘echo’, [‘Hello’]), produces a different error:

** (FunctionClauseError) no function clause matching in System.cmd/3

    The following arguments were given to System.cmd/3:

        # 1
        'echo'

        # 2
        ['Hello']

        # 3
        []

    Attempted function clauses (showing 1 out of 1):

        def cmd(+command+, +args+, +opts+) when -is_binary(command)- and +is_list(args)+

    (elixir 1.10.4) lib/system.ex:782: System.cmd/3
    (elixir 1.10.4) lib/code.ex:926: Code.require_file/2

probably unrelated although I’m guessing this means that double quotes and single quotes are different in elixir. The other thing is that the following command works:

IO.puts "echo Hello" |> String.to_charlist |> :os.cmd

Although this one seems to return the result of running the command instead of directly running it in the running shell, hence the IO.puts, and I assume this isn’t the same behaviour as System.cmd
My apologies for the long post, but hopefully we can clear some things up here, and thanks in advance

Marked As Solved

NobbZ

NobbZ

In windows echo as well as dir are strict built-in a, there is no way to call them as binary.

You probably need to do something like System.cmd("cmd", ["-c", "echo foo"]) or something like that.

Also Liked

derek-zhou

derek-zhou

The first thing I do on Windows is to install msys2 which gives you a sane command line interface.

Cmd is dead. PowerShell is nice however it is not significantly better than bash so I don’t see the point of being different from everybody else.

NobbZ

NobbZ

Yes, by calling Powershell instead of cmd and passing it the command/script with appropriate command line flags.

Last Post!

derek-zhou

derek-zhou

The first thing I do on Windows is to install msys2 which gives you a sane command line interface.

Cmd is dead. PowerShell is nice however it is not significantly better than bash so I don’t see the point of being different from everybody else.

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