tcoopman
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))
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.
idi527
Or
:os.cmd('sleep 0.1 | echo foo').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
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.cmdis 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.alco
It is strange that
:os.cmdaccepts an atom for the command name, looks like a historical accident.In general, functions from the Erlang standard library take character lists where in Elixir you would use a string. To make it more confusing, the Erlang type for such lists is called
string(). I recommend taking a look at this cheat sheet if you still feel confused.Going back to your problem, Ben has already done a good job explaining the difference between Elixir’s
System.cmdand Erlang’s:os.cmd. In the source code for:os.cmdyou can see it actually spawns a shell appropriate for the current OS and evaluates the string you’ve passed to the function in that shell.Replicating this behaviour for your particular example using
System.cmdis easy:OvermindDL1
Not entirely. A lot of the ‘base’ functions get the string value of atoms, it makes it cheap and efficient to pass around something like
sedinstead of having to do"sed"in the original erlang (plus it’s fantastic since atoms in erlang can have@in them without quoting too!). Calling other programs was generally used to access their stdin/stdout, not for argument parsing and ‘doing’ other stuff.(Do note, the
sedand"sed"above in erlang, in elixir would be:sedand'sed'respectively.)Qqwy
Why is this an advantage for calling functions that happen to also accept atoms and not only charlists?
OvermindDL1
Overall less space for repeatedly used ‘strings’, atoms are essentially interned/flyweight’d strings after all (and lists of integers are anything but lightweight in memory, even if they are very fast iterables). Not really an issue nowadays, but Erlang is old remember. ^.^
As for the
@bit, it’s because they are used in areas on the VM such as server names, cookies, etc… Like in elixir you’d have to do:"username@server"where in erlang you’d just dousername@server, nothing else needed, which is a great boon when manually doing a lot of work at the console or putting those in erlang config files. But of course, the ecosystem handles that better now and so things like Elixir don’t even support@in unquoted atoms unlike Erlang. ^.^;tcoopman
System.cmd("sh", ["sleep 0.1"])results in/usr/bin/sh: sleep 0.1: No such file or directory {"", 127}. So this solution doesn’t seem to workalco
I made a mistake in the invocation. To evaluate a string in
sh, the-chas to be passed first: