Book Image

Elixir Cookbook

By : Paulo Pereira
Book Image

Elixir Cookbook

By: Paulo Pereira

Overview of this book

Table of Contents (16 chapters)
Elixir Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Create a new application:

    > mix new os_commands
    
  2. Add the porcelain app as a dependency in the mix.exs file:

    defp deps do
      [{:porcelain, "~> 2.0"}]
    end
  3. Register porcelain into the list of applications (inside the mix.exs file):

    def application do
      [applications: [:logger, :porcelain]]
    end
  4. Get the...