Using OS commands from within Elixir
It is possible to interact with the underlying operating system, execute OS commands, and get the result in our Elixir applications.
To do this, we will be using Alexei Sholik's porcelain (https://hex.pm/packages/porcelain).
We will build a very simple application that will accept a string defining a path and will return a list containing the entries for that path. We will use the ls unix
command without leaving our Elixir application! We will also define a generic run function that will allow the running of any command we pass as the argument.
How to do it…
To create an application that interacts with the underlying operating system, we will follow these steps:
Create a new application:
> mix new os_commands
Add the porcelain app as a dependency in the
mix.exs
file:defp deps do [{:porcelain, "~> 2.0"}] end
Register porcelain into the list of applications (inside the
mix.exs
file):def application do [applications: [:logger, :porcelain]] end
Get the...