tcoopman

tcoopman

Run a shell command with a pipe operator

I’m trying to run a shell command with a pipe operator in it. For example “sleep 0.1 | echo foo”. At first I tried to do this with System.cmd:

System.cmd("sleep", ["0.1", "|", "echo", "foo"])

But this results in /usr/bin/sleep: invalid time interval '|', invalid time interval 'echo'....

So next I tried to use :os.cmd:

:os.cmd("sleep 0.1 | echo foo")

But whatever I do to execute this I get:

:os.cmd("sleep 0.1 | echo foo")
** (FunctionClauseError) no function clause matching in :os.validate/1    
    
    The following arguments were given to :os.validate/1:
    
        # 1
        "sleep 0.1 | echo foo"
    
    (kernel) os.erl:281: :os.validate/1
    (kernel) os.erl:237: :os.cmd/1

Looking into the code of :os (otp/lib/kernel/src/os.erl at master · erlang/otp · GitHub) is see that :os.validate expects atoms? But in the erlang docs there are no atoms used (os — OTP 29.0.2 (kernel 11.0.2))

Marked As Solved

tcoopman

tcoopman

I’ve just been able to solve my own problem.
If I do: os.cmd(:"sleep 0.1 | echo foo") it works.

So I need to pass an atom to os.cmd.

Also Liked

alco

alco

I made a mistake in the invocation. To evaluate a string in sh, the -c has to be passed first:

System.cmd("sh", ["-c", "sleep 0.1"])
idi527

idi527

Or :os.cmd('sleep 0.1 | echo foo').

iex(1)> :os.cmd('sleep 0.1 | echo foo')
'foo\n'

It works with atoms and character lists. Doesn’t seem to work with binaries.

I would try to avoid creating new atoms for each command you might want to run since they are not garbage collected.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

System.cmd does not run a shell, so pipes, redirects, and so on aren’t supported. System.cmd is basically the closest thing that Elixir has to fork.

:os.cmd is definitely handy, but take great care if any of the stuff in your command comes from users (like filenames). You risk letting users run arbitrary programs.

Last Post!

alco

alco

I made a mistake in the invocation. To evaluate a string in sh, the -c has to be passed first:

System.cmd("sh", ["-c", "sleep 0.1"])

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
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
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
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

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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
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