SETENVIRON

Linux specific questions.
jdmcbride
Posts: 27
Joined: Aug 06, 2016 16:13

SETENVIRON

Postby jdmcbride » Oct 20, 2020 22:42

in Linux, I can set an environment variable with:

SETENVIRON("COUNT=123")
print ENVIRON("COUNT") will return 123 as a string

so far so good....

Doing this fails:

MYCOUNT="123"

SETENVIRON("COUNT=" + MYCOUNT)
print ENVIRON("COUNT") will return a NULL

Why is that?
grindstone
Posts: 755
Joined: May 05, 2015 5:35
Location: Germany

Re: SETENVIRON

Postby grindstone » Oct 21, 2020 9:10

In Windows it works.
Landeel
Posts: 752
Joined: Jan 25, 2007 10:32
Location: Brazil
Contact:

Re: SETENVIRON

Postby Landeel » Oct 21, 2020 19:05

This works:

Code: Select all

dim as string MYCOUNT="COUNT=123"
SETENVIRON(MYCOUNT)
print ENVIRON("COUNT")


This fails:

Code: Select all

dim as string MYCOUNT="123"
SETENVIRON("COUNT=" + MYCOUNT)
print ENVIRON("COUNT") 'will return a NULL


Looks like a bug to me.
fxm
Posts: 9993
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SETENVIRON

Postby fxm » Oct 21, 2020 19:13

It works with Windows.

It can't hurt to try this too:

Code: Select all

dim as string MYCOUNT="123"
SETENVIRON("COUNT=" & MYCOUNT)
print ENVIRON("COUNT") 'will return a NULL
Landeel
Posts: 752
Joined: Jan 25, 2007 10:32
Location: Brazil
Contact:

Re: SETENVIRON

Postby Landeel » Oct 21, 2020 19:25

fxm wrote:

Code: Select all

dim as string MYCOUNT="123"
SETENVIRON("COUNT=" & MYCOUNT)
print ENVIRON("COUNT") 'will return a NULL


Fails too!
fxm
Posts: 9993
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SETENVIRON

Postby fxm » Oct 21, 2020 19:31

and this?

Code: Select all

dim as string MYCOUNT="123"
SETENVIRON(("COUNT=" + MYCOUNT))
print ENVIRON("COUNT") 'will return a NULL
badidea
Posts: 2152
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: SETENVIRON

Postby badidea » Oct 21, 2020 20:34

fxm wrote:and this?

Code: Select all

dim as string MYCOUNT="123"
SETENVIRON(("COUNT=" + MYCOUNT))
print ENVIRON("COUNT") 'will return a NULL
Fail

Code: Select all

dim as string MYCOUNT=""
SETENVIRON("COUNT=123" & MYCOUNT)
print ENVIRON("COUNT")
Fail

Code: Select all

dim as string MYCOUNT=""
SETENVIRON(MYCOUNT & "COUNT=123")
print ENVIRON("COUNT")
Fail

Code: Select all

SETENVIRON("COUNT=123" & "")
print ENVIRON("COUNT")
Pass

FreeBASIC Compiler - Version 1.07.1 (2019-09-27), built for linux-x86 (32bit)
and
FreeBASIC Compiler - Version 1.07.1 (2019-09-27), built for linux-x86_64 (64bit)
fxm
Posts: 9993
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SETENVIRON

Postby fxm » Oct 21, 2020 21:22

Look at SetEnviron troubles

When using 'SETENVIRON(envStr)' with Linux, the 'envStr' string must be permanent (a literal, a variable declared in the main code or a static variable declared in a procedure), because Linux does not memorize the string but only a pointer to its data characters.

Short example:

Code: Select all

dim as string MYCOUNT="123"
dim shared as string envStr1
envStr1 = "COUNT=" + MYCOUNT  '' envStr1 must always exist without ever being modified
SETENVIRON(envStr1)
print ENVIRON("COUNT")

If it works, I would add a note (inside paragraph "Platform Differences") in the documentation.
Last edited by fxm on Oct 22, 2020 9:23, edited 6 times in total.
badidea
Posts: 2152
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: SETENVIRON

Postby badidea » Oct 21, 2020 22:08

Ah, that make sense.

Code: Select all

sub set()
   dim as string MYCOUNT="123"
   dim as string envStr1
   envStr1 = "COUNT=" + MYCOUNT  '' envStr1 must always exist without ever being modified
   SETENVIRON(envStr1)
end sub

set()
print ENVIRON("COUNT")
Fail

Code: Select all

dim shared as string envStr1

sub set()
   dim as string MYCOUNT="123"
   envStr1 = "COUNT=" + MYCOUNT  '' envStr1 must always exist without ever being modified
   SETENVIRON(envStr1)
end sub

set()
print ENVIRON("COUNT")
Pass
hhr
Posts: 10
Joined: Nov 29, 2019 10:41

Re: SETENVIRON

Postby hhr » Oct 22, 2020 6:58

Code: Select all

Setenviron("COUNT=123")
Print Environ("COUNT")

Const As String MYCOUNT1="456"
Setenviron("COUNT=" + MYCOUNT1)
Print Environ("COUNT")

Dim As String MYCOUNT2="789"
Dim As String envStr="COUNT=" + MYCOUNT2
Setenviron(envStr)
Print Environ("COUNT")

Sleep
fxm
Posts: 9993
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SETENVIRON

Postby fxm » Oct 22, 2020 7:23

'Dim As String envStr' works only when this declaration is done in the main code.
fxm
Posts: 9993
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SETENVIRON

Postby fxm » Oct 22, 2020 8:08

fxm wrote:If it works, I would add a note (inside paragraph "Platform Differences") in the documentation.

Done:
KeyPgSetenviron → fxm [In Linux, 'SetEnviron' memorizes a pointer to the string data characters and not the string itself]
hhr
Posts: 10
Joined: Nov 29, 2019 10:41

Re: SETENVIRON

Postby hhr » Oct 22, 2020 8:56

I don't understand 'never be modified':

Code: Select all

Dim As String MYCOUNT,envStr

MYCOUNT="123"
envStr="COUNT=" + MYCOUNT
Setenviron(envStr)
Print Environ("COUNT")

MYCOUNT="ABCDEF"
envStr="COUNT=" + MYCOUNT
Setenviron(envStr)
Print Environ("COUNT")

Sleep
fxm
Posts: 9993
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: SETENVIRON

Postby fxm » Oct 22, 2020 9:08

Try this in Linux (I do not have Linux):

Code: Select all

Dim As String MYCOUNT,envStr

MYCOUNT="123"
envStr="COUNT=" + MYCOUNT
Setenviron(envStr)
Print Environ("COUNT")

envStr="modified"
Print Environ("COUNT")

Sleep

I now think that the denomination of "permanent" is sufficient, given the following explanation about memorizing a pointer only?
hhr
Posts: 10
Joined: Nov 29, 2019 10:41

Re: SETENVIRON

Postby hhr » Oct 22, 2020 10:15

Thank you. Output in Linux:

Code: Select all

123       Len=3
          Len=0

Return to “Linux”

Who is online

Users browsing this forum: No registered users and 2 guests