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:

1 Like

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
8 Likes