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.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement