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
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
NobbZ
Yes, by calling Powershell instead of cmd and passing it the command/script with appropriate command line flags.
Last Post!
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









