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"])
10
Post #9
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.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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

We're in Beta

About us Mission Statement