Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Elixir Cookbook

You're reading from   Elixir Cookbook Unleash the full power of programming in Elixir with over 60 incredibly effective recipes

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher Packt
ISBN-13 9781784397517
Length 236 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Paulo Pereira Paulo Pereira
Author Profile Icon Paulo Pereira
Paulo Pereira
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Command Line 2. Data Types and Structures FREE CHAPTER 3. Strings and Binaries 4. Modules and Functions 5. Processes and Nodes 6. OTP – Open Telecom Platform 7. Cowboy and Phoenix 8. Interactions A. Installation and Further Reading 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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime