zpeters

zpeters

System.cmd results as a string?

I feel like i’m missing something really basic here but for the life of me cannot figure this out.

When i run System.cmd(“echo”, [“hello”]). I get the output i expect, namely {“hello\n”, 0}

However when i run System.cmd(“lynx”, [“–dump”, “http://google.com”]) I get something like this

{<<10, 32, 32, 32, 83, 101, 97, 114, 99, 104, 32, 91, 49, 93, 73, 109, 97, 103,
101, 115, 32, 91, 50, 93, 77, 97, 112, 115, 32, 91, 51, 93, 80, 108, 97, 121,
32, 91, 52, 93, 89, 111, 117, 84, 117, 98, 101, 32, 91, …>>, 0}

When i run lynx --dump http://google.com from a shell it appears to be a normal string that is output. I don’t understand why running this command in elixir gives me a binary result.

If this is unavoidable, is there a way to convert from this binary to a string?

Thank you in advance for the schooling! :slight_smile:

Most Liked

axelson

axelson

Scenic Core Team

A String in Elixir is actually a UTF-8 encoded binary: https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html

So both of your results are binaries. One way to verify/play with this is to run

iex(30)> i "hello\n"
Term
  "hello\n"
Data type
  BitString
Byte size
  6
Description
  This is a string: a UTF-8 encoded binary. It's printed surrounded by
  "double quotes" because all UTF-8 encoded codepoints in it are printable.
Raw representation
  <<104, 101, 108, 108, 111, 10>>
Reference modules
  String, :binary
Implemented protocols
  Collectable, Ecto.DataType, Ecto.Queryable, IEx.Info, Inspect, List.Chars, Msgpax.Packer, Phoenix.HTML.Safe, Phoenix.Param, Plug.Exception, Poison.Decoder, Poison.Encoder, Slugify, String.Chars, Timber.Eventable

From there you can see the raw “binary” representation is <<104, 101, 108, 108, 111, 10>> and if you enter that into an iEX prompt you get back the string “hello\n” (note: this only works for binaries that are valid UTF-8 and all the characters are printable)

iex(31)> <<104, 101, 108, 108, 111, 10>>
"hello\n"

So for some reason lynx is returning invalid UTF-8 when run as lynx --dump http://google.com. To strip invalid characters you could do some processing like so:

iex(39)> {string, _} = System.cmd("lynx", ["--dump", "http://google.com"])
{<<10, 32, 32, 32, 83, 101, 97, 114, 99, 104, 32, 91, 49, 93, 73, 109, 97, 103,
   101, 115, 32, 91, 50, 93, 77, 97, 112, 115, 32, 91, 51, 93, 80, 108, 97, 121,
   32, 91, 52, 93, 89, 111, 117, 84, 117, 98, 101, 32, 91, ...>>, 0}
iex(40)> String.graphemes(string) |> Enum.filter(&String.valid?/1) |> Enum.join("") |> IO.puts

   Search [1]Images [2]Maps [3]Play [4]YouTube [5]News [6]Gmail [7]Drive
   [8]More
   [9]Web History | [10]Settings | [11]Sign in

   [12]Day 5 of the Doodle Snow Games!

     _______________________________________________________
   Google Search  I'm Feeling Lucky    [13]Advanced search
      [14]Language tools

   [15]Advertising Programs     [16]Business Solutions     [17]+Google
    [18]About Google

                       2018 - [19]Privacy - [20]Terms

References

   1. http://www.google.com/imghp?hl=en&tab=wi
   2. http://maps.google.com/maps?hl=en&tab=wl
   3. https://play.google.com/?hl=en&tab=w8
   4. http://www.youtube.com/?gl=US&tab=w1
   5. http://news.google.com/nwshp?hl=en&tab=wn
   6. https://mail.google.com/mail/?tab=wm
   7. https://drive.google.com/?tab=wo
   8. https://www.google.com/intl/en/options/
   9. http://www.google.com/history/optout?hl=en
  10. http://www.google.com/preferences?hl=en
  11. https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=http://www.google.com/
  12. http://www.google.com/search?site=&ie=UTF-8&q=Winter+Olympics&oi=ddle&ct=doodle-snow-games-day-7-5009413877268480-lawcta&hl=en&kgmid=/m/03tng8&sa=X&ved=0ahUKEwi5q-6Bo6TZAhVY62MKHdNJB3wQPQgC
  13. http://www.google.com/advanced_search?hl=en&authuser=0
  14. http://www.google.com/language_tools?hl=en&authuser=0
  15. http://www.google.com/intl/en/ads/
  16. http://www.google.com/services/
  17. https://plus.google.com/116899029375914044550
  18. http://www.google.com/intl/en/about.html
  19. http://www.google.com/intl/en/policies/privacy/
  20. http://www.google.com/intl/en/policies/terms/

:ok

Where Next?

Popular in Questions Top

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
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement