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

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.

beardedeagle

beardedeagle

@isaacsanders & @peerreynders

iex(16)> port = Port.open({:spawn, "/bin/echo $GOPATH"}, [:binary])
#Port<0.19>
iex(17)> flush
{#Port<0.19>, {:data, "/Users/rlthompson/go\n"}}

:spawn vs :spawn_executable. Now I feel dumb.

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

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

We're in Beta

About us Mission Statement