beardedeagle

beardedeagle

Use env variables with System.cmd or Ports?

I’m trying to figure out how to be able to expand ENV variables in either Port or System.cmd. Porcelain seems to be capable of this, but I cannot do it with either Port or System.cmd:

iex(5)> System.cmd("/bin/echo", ["$GOPATH"])
{"$GOPATH\n", 0}
iex(6)> port = Port.open({:spawn_executable, "/bin/echo"}, [:binary, args: ["$GOPATH"]])
#Port<0.10>
iex(7)> flush()
{#Port<0.10>, {:data, "$GOPATH\n"}}
:ok
Porcelain.shell("echo $GOPATH")
%Porcelain.Result{err: nil, out: "/Users/rlthompson/go\n", status: 0}

The issue is Porcelain is in a state where I am uncomfortable using it in production (it’s basically abandonware in my personal opinion). I tried taking a look at how Porcelain is accomplishing this, but was unable to make heads or tails of the specific calls they make.

Any help, direction or docs to be pointed at would be greatly appreciated. Thank you in advance.

Marked As Solved

beardedeagle

beardedeagle

And if anyone comes to this thread looking for the same thing i was, here is how you also get the status code:

iex(18)> port = Port.open({:spawn, "/bin/echo $GOPATH"}, [:binary, :exit_status])
#Port<0.20>
iex(19)> flush
{#Port<0.20>, {:data, "/Users/rlthompson/go\n"}}
{#Port<0.20>, {:exit_status, 0}}
:ok

Also Liked

evhinesdev

evhinesdev

I know this is ancient but ended up here on the same adventure. I see you solved it with a port but you can do this with a System.cmd with an inline “sh -c ‘CMD’” script:

 System.cmd("sh", ["-c", "echo $GOPATH"], env: [{"GOPATH", "/Users/rlthompson/go"}])
lpil

lpil

Creator of Gleam

What makes you say this? It’s a well defined library that could be considered complete. It’s not like the *nix CLI is a moving target that the library needs to be updated to work with :slight_smile:

isaacsanders

isaacsanders

If you are trying to get a subset of your environment sent to the port, I would do so explicitly. I am under the impression from what you have posted that it doesn’t look at parameters in your shell, but requires you to set the env for the shell it creates.

Last Post!

evhinesdev

evhinesdev

I know this is ancient but ended up here on the same adventure. I see you solved it with a port but you can do this with a System.cmd with an inline “sh -c ‘CMD’” script:

 System.cmd("sh", ["-c", "echo $GOPATH"], env: [{"GOPATH", "/Users/rlthompson/go"}])

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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 31494 112
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement