RE: A way to export from c an environment var

2008-12-17 Thread Tivy, Robert
No, a process can't modify the environment of its parent (and the parent of a 
program is typically a shell).  You could have the program output the desired 
environment variable contents and do
% YOURENVVAR=`yourprogramname`

- Rob 

> -Original Message-
> From: 
> davinci-linux-open-source-bounces+rtivy=ti@linux.davincids
> p.com 
> [mailto:davinci-linux-open-source-bounces+rtivy=ti@linux.d
> avincidsp.com] On Behalf Of Van Vliet
> Sent: Wednesday, December 17, 2008 7:57 AM
> To: davinci-linux-open-source@linux.davincidsp.com
> Subject: A way to export from c an environment var
> 
> Hi,
> 
> Is there a way to create and export from c an environment 
> variable a bash script can be aware of? I intend something 
> like PATH, etc.
> 
> Thanks and Regards,
> 
> VanVliet
> 
> Dr. Caroline Van Vliet
> Adfl Group
> Advanced DSP Flexible Language Group
> Via Greto di Cornigliano 6R
> 16152 - Genova, Italy
> Tel. +39 010 8609370 - Fax +39 010 8609381
> 
> 
> ___
> Davinci-linux-open-source mailing list
> Davinci-linux-open-source@linux.davincidsp.com
> http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source
> ___
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source


RE: A way to export from c an environment var

2008-12-17 Thread Sander Huijsen
You might want to try setenv(). Just type 'man 3 setenv' in a Linux
shell to get the man-page for that function (which is in stdlib). 

Sander


-Original Message-
From:
davinci-linux-open-source-bounces+shuijsen=optelecom-nkf@linux.davin
cidsp.com
[mailto:davinci-linux-open-source-bounces+shuijsen=optelecom-nkf@lin
ux.davincidsp.com] On Behalf Of Van Vliet
Sent: Wednesday, December 17, 2008 4:57 PM
To: davinci-linux-open-source@linux.davincidsp.com
Subject: A way to export from c an environment var

Hi,

Is there a way to create and export from c an environment
variable a bash
script can be aware of? I intend something like PATH, etc.

Thanks and Regards,

VanVliet

Dr. Caroline Van Vliet
Adfl Group
Advanced DSP Flexible Language Group
Via Greto di Cornigliano 6R
16152 - Genova, Italy
Tel. +39 010 8609370 - Fax +39 010 8609381


___
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source
  


 
CONFIDENTIALITY NOTICE - This e-mail transmission, and any documents, files or 
previous e-mail messages attached to it may contain information that is 
confidential or legally privileged. If you are not the intended recipient, or a 
person responsible for delivering it to the intended recipient, you are hereby 
notified that you must not read this transmission and that any disclosure, 
copying, printing, distribution or use of any of the information contained in 
or attached to this transmission is STRICTLY PROHIBITED. If you have received 
this transmission in error, please immediately notify Sander Huijsen by 
telephone or shuij...@optelecom-nkf.com and delete the original transmission 
and its attachments without reading or saving in any manner.

___
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source


Re: A way to export from c an environment var

2008-12-18 Thread Arie de Muijnck

Unless one wants to set a persistant value, that can be used after his/hers
process finishes...

Unfortunately functions like setenv(), putenv(), system("export ...") change
only the current environment of the process itself, not the 'global'
environment of the user (let alone the system).

Regards,
Arie de Muijnck

- Original Message - 

You might want to try setenv(). Just type 'man 3 setenv' in a Linux
shell to get the man-page for that function (which is in stdlib).

Sander


-Original Message-
Hi,

Is there a way to create and export from c an environment
variable a bash
script can be aware of? I intend something like PATH, etc.

Thanks and Regards,

VanVliet

Dr. Caroline Van Vliet
Adfl Group
Advanced DSP Flexible Language Group
Via Greto di Cornigliano 6R
16152 - Genova, Italy
Tel. +39 010 8609370 - Fax +39 010 8609381



___
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source


Re: A way to export from c an environment var

2008-12-18 Thread Phil Quiney

Hi,

I have tried some experiments along these lines.

#include 
#include 

int main(int argc, char *argv[])
{
   char *rc;
   FILE *fp;
   char buffer[256];

   putenv("FRED=\"Hello World\"");

   rc = getenv("FRED");

   if(rc)
   printf("rc: %s\n", rc);
   else
   printf("Not found\n");


   fp = popen("export FRED=\"Goodbye\"", "r");

   pclose(fp);

   fp = popen("env|grep FRED", "r");

   if(fp)
   {
   rc = fgets(buffer, sizeof(buffer), fp);
   if(rc)
   printf("returned: %s\n", buffer);

   pclose(fp);

   }
   return 0;
}

Checking after the program exits, it proves Rob Tivy is right (no 
FRED defined). The variable is set only in the environment space of the 
running program it cannot affect the parent process.


What was interesting is the 'popen' to set FRED="Goodbye"  did not 
persist, presumably it only existed in the shell created to execute the 
command. The 'popen' to get the value of FRED returned the 'hello world' 
set by 'putenv'.


Regards

Phil Q

Sander Huijsen wrote:


You might want to try setenv(). Just type 'man 3 setenv' in a Linux
shell to get the man-page for that function (which is in stdlib). 


Sander


-Original Message-
From:
davinci-linux-open-source-bounces+shuijsen=optelecom-nkf@linux.davin
cidsp.com
[mailto:davinci-linux-open-source-bounces+shuijsen=optelecom-nkf@lin
ux.davincidsp.com] On Behalf Of Van Vliet
Sent: Wednesday, December 17, 2008 4:57 PM
To: davinci-linux-open-source@linux.davincidsp.com
Subject: A way to export from c an environment var

Hi,

Is there a way to create and export from c an environment
variable a bash
script can be aware of? I intend something like PATH, etc.

Thanks and Regards,

VanVliet

Dr. Caroline Van Vliet
Adfl Group
Advanced DSP Flexible Language Group
Via Greto di Cornigliano 6R
16152 - Genova, Italy
Tel. +39 010 8609370 - Fax +39 010 8609381


___
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source
 




CONFIDENTIALITY NOTICE - This e-mail transmission, and any documents, files or 
previous e-mail messages attached to it may contain information that is 
confidential or legally privileged. If you are not the intended recipient, or a 
person responsible for delivering it to the intended recipient, you are hereby 
notified that you must not read this transmission and that any disclosure, 
copying, printing, distribution or use of any of the information contained in 
or attached to this transmission is STRICTLY PROHIBITED. If you have received 
this transmission in error, please immediately notify Sander Huijsen by 
telephone or shuij...@optelecom-nkf.com and delete the original transmission 
and its attachments without reading or saving in any manner.

___
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source
 




___
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source


RE: A way to export from c an environment var

2008-12-18 Thread Sander Huijsen
Well yes, Arie, you are right about that. I did find a clever/nasty (you
be the judge of that) solution for the problem. Simply write the
environment variable from within your application to a file, and then
"source" the file in your script. :-)

The script might look something like this:

[script]
#!/bin/sh

# create some environment variable 'EVR' and show it
EVR="your value"
echo $EVR

# run your C program, which changes EVR and exports it to
/tmp/evr.env
./your_c_program

# source the changed environment
source /tmp/evr.env

# now show the changed value
echo $EVR

# and exit the script.
exit 0
[/script]


In your C program, you would do something like this: 

[snippet]
// Here, you change the environment variable
setenv("EVR", "my changed value", 1);

// Open a temp file to write the environment variable in
FILE *fh = fopen("/tmp/evr.env", "w+");
if (fh)
{
// This will write 'EVR="my changed value"' in the temp
file
fprintf(fh, "EVR=\"%s\"\n", getenv("EVR"));
fclose(fh);
}
[/snippet]


Regards,
Sander


-Original Message-
From: Arie de Muijnck [mailto:leon...@ademu.com] 
Sent: Thursday, December 18, 2008 10:52 AM
To: Sander Huijsen; davinci-linux-open-source@linux.davincidsp.com
Subject: Re: A way to export from c an environment var

Unless one wants to set a persistant value, that can be used after
his/hers
process finishes...

Unfortunately functions like setenv(), putenv(), system("export ...")
change
only the current environment of the process itself, not the 'global'
environment of the user (let alone the system).

Regards,
Arie de Muijnck

- Original Message - 
> You might want to try setenv(). Just type 'man 3 setenv' in a Linux
> shell to get the man-page for that function (which is in stdlib).
>
> Sander
>
>
> -Original Message-
> Hi,
>
> Is there a way to create and export from c an environment
> variable a bash
> script can be aware of? I intend something like PATH, etc.
>
> Thanks and Regards,
>
> VanVliet
>
> Dr. Caroline Van Vliet
> Adfl Group
> Advanced DSP Flexible Language Group
> Via Greto di Cornigliano 6R
> 16152 - Genova, Italy
> Tel. +39 010 8609370 - Fax +39 010 8609381
  


 
CONFIDENTIALITY NOTICE - This e-mail transmission, and any documents, files or 
previous e-mail messages attached to it may contain information that is 
confidential or legally privileged. If you are not the intended recipient, or a 
person responsible for delivering it to the intended recipient, you are hereby 
notified that you must not read this transmission and that any disclosure, 
copying, printing, distribution or use of any of the information contained in 
or attached to this transmission is STRICTLY PROHIBITED. If you have received 
this transmission in error, please immediately notify Sander Huijsen by 
telephone or shuij...@optelecom-nkf.com and delete the original transmission 
and its attachments without reading or saving in any manner.

___
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source


RE: A way to export from c an environment var

2008-12-18 Thread Tivy, Robert
This is overly complex.  The initial setting of env var EVR at the beginning of 
the script does nothing, and the C program is just using the environment to 
store a temp value, which isn't even needed.  The usage of a file is also 
unnecessary (unless you want to retain the value).

This would do the same thing...
#!/bin/sh
EVR=`./your_c_program`

Or in csh
#!/bin/csh
setenv EVR `./your_c_program`

With the C program being
printf("%d", int_value);
or
printf(string_value);
or whatever needs to be set in the env var.

- Rob

> -Original Message-
> From: 
> davinci-linux-open-source-bounces+rtivy=ti@linux.davincids
> p.com 
> [mailto:davinci-linux-open-source-bounces+rtivy=ti@linux.d
> avincidsp.com] On Behalf Of Sander Huijsen
> Sent: Thursday, December 18, 2008 6:08 AM
> To: davinci-linux-open-source@linux.davincidsp.com
> Subject: RE: A way to export from c an environment var
> 
> Well yes, Arie, you are right about that. I did find a 
> clever/nasty (you be the judge of that) solution for the 
> problem. Simply write the environment variable from within 
> your application to a file, and then "source" the file in 
> your script. :-)
> 
> The script might look something like this:
> 
> [script]
>   #!/bin/sh
> 
>   # create some environment variable 'EVR' and show it
>   EVR="your value"
>   echo $EVR
> 
>   # run your C program, which changes EVR and exports it 
> to /tmp/evr.env
>   ./your_c_program
> 
>   # source the changed environment
>   source /tmp/evr.env
> 
>   # now show the changed value
>   echo $EVR
> 
>   # and exit the script.
>   exit 0
> [/script]
> 
> 
> In your C program, you would do something like this: 
> 
> [snippet]
>   // Here, you change the environment variable
>   setenv("EVR", "my changed value", 1);
> 
>   // Open a temp file to write the environment variable in
>   FILE *fh = fopen("/tmp/evr.env", "w+");
>   if (fh)
>   {
>   // This will write 'EVR="my changed value"' in 
> the temp file
>   fprintf(fh, "EVR=\"%s\"\n", getenv("EVR"));
>       fclose(fh);
>       }
> [/snippet]
> 
> 
> Regards,
> Sander
> 
> 
> -Original Message-
> From: Arie de Muijnck [mailto:leon...@ademu.com]
> Sent: Thursday, December 18, 2008 10:52 AM
> To: Sander Huijsen; davinci-linux-open-source@linux.davincidsp.com
> Subject: Re: A way to export from c an environment var
> 
> Unless one wants to set a persistant value, that can be used 
> after his/hers process finishes...
> 
> Unfortunately functions like setenv(), putenv(), 
> system("export ...") change only the current environment of 
> the process itself, not the 'global'
> environment of the user (let alone the system).
> 
> Regards,
> Arie de Muijnck
> 
> - Original Message - 
> > You might want to try setenv(). Just type 'man 3 setenv' in a Linux 
> > shell to get the man-page for that function (which is in stdlib).
> >
> > Sander
> >
> >
> > -Original Message-
> > Hi,
> >
> > Is there a way to create and export from c an environment 
> variable a 
> > bash script can be aware of? I intend something like PATH, etc.
> >
> > Thanks and Regards,
> >
> > VanVliet
> >
> > Dr. Caroline Van Vliet
> > Adfl Group
> > Advanced DSP Flexible Language Group
> > Via Greto di Cornigliano 6R
> > 16152 - Genova, Italy
> > Tel. +39 010 8609370 - Fax +39 010 8609381
>   
> 
> 
>  
> CONFIDENTIALITY NOTICE - This e-mail transmission, and any 
> documents, files or previous e-mail messages attached to it 
> may contain information that is confidential or legally 
> privileged. If you are not the intended recipient, or a 
> person responsible for delivering it to the intended 
> recipient, you are hereby notified that you must not read 
> this transmission and that any disclosure, copying, printing, 
> distribution or use of any of the information contained in or 
> attached to this transmission is STRICTLY PROHIBITED. If you 
> have received this transmission in error, please immediately 
> notify Sander Huijsen by telephone or 
> shuij...@optelecom-nkf.com and delete the original 
> transmission and its attachments without reading or saving in 
> any manner.
> 
> ___
> Davinci-linux-open-source mailing list
> Davinci-linux-open-source@linux.davincidsp.com
> http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source
> ___
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source


RE: A way to export from c an environment var

2008-12-19 Thread Sander Huijsen
I guess there's more than one way to solve a problem. :) 

The initial setting of EVR was there just to show that you can also use the 
variable in the C program using getenv(). Your solution works fine if there's 
only one env var to set (apart from some other practical problems that may 
arise). When you want to set multiple env vars, it wouldn't work. 

But I must agree, since the initial question only mentioned one env var, your 
solution is the simpler one. :) 

Kind regards,
Sander



-Oorspronkelijk bericht-
Van: Tivy, Robert [mailto:rt...@ti.com]
Verzonden: do 18-12-2008 13:02
Aan: Sander Huijsen; davinci-linux-open-source@linux.davincidsp.com
Onderwerp: RE: A way to export from c an environment var
 
This is overly complex.  The initial setting of env var EVR at the beginning of 
the script does nothing, and the C program is just using the environment to 
store a temp value, which isn't even needed.  The usage of a file is also 
unnecessary (unless you want to retain the value).

This would do the same thing...
#!/bin/sh
EVR=`./your_c_program`

Or in csh
#!/bin/csh
setenv EVR `./your_c_program`

With the C program being
printf("%d", int_value);
or
printf(string_value);
or whatever needs to be set in the env var.

- Rob

> -Original Message-
> From: 
> davinci-linux-open-source-bounces+rtivy=ti@linux.davincids
> p.com 
> [mailto:davinci-linux-open-source-bounces+rtivy=ti@linux.d
> avincidsp.com] On Behalf Of Sander Huijsen
> Sent: Thursday, December 18, 2008 6:08 AM
> To: davinci-linux-open-source@linux.davincidsp.com
> Subject: RE: A way to export from c an environment var
> 
> Well yes, Arie, you are right about that. I did find a 
> clever/nasty (you be the judge of that) solution for the 
> problem. Simply write the environment variable from within 
> your application to a file, and then "source" the file in 
> your script. :-)
> 
> The script might look something like this:
> 
> [script]
>   #!/bin/sh
> 
>   # create some environment variable 'EVR' and show it
>   EVR="your value"
>   echo $EVR
> 
>   # run your C program, which changes EVR and exports it 
> to /tmp/evr.env
>   ./your_c_program
> 
>   # source the changed environment
>   source /tmp/evr.env
> 
>   # now show the changed value
>   echo $EVR
> 
>   # and exit the script.
>   exit 0
> [/script]
> 
> 
> In your C program, you would do something like this: 
> 
> [snippet]
>   // Here, you change the environment variable
>   setenv("EVR", "my changed value", 1);
> 
>   // Open a temp file to write the environment variable in
>   FILE *fh = fopen("/tmp/evr.env", "w+");
>   if (fh)
>   {
>   // This will write 'EVR="my changed value"' in 
> the temp file
>   fprintf(fh, "EVR=\"%s\"\n", getenv("EVR"));
>       fclose(fh);
>   }
> [/snippet]
> 
> 
> Regards,
> Sander
> 
> 
> -Original Message-
> From: Arie de Muijnck [mailto:leon...@ademu.com]
> Sent: Thursday, December 18, 2008 10:52 AM
> To: Sander Huijsen; davinci-linux-open-source@linux.davincidsp.com
> Subject: Re: A way to export from c an environment var
> 
> Unless one wants to set a persistant value, that can be used 
> after his/hers process finishes...
> 
> Unfortunately functions like setenv(), putenv(), 
> system("export ...") change only the current environment of 
> the process itself, not the 'global'
> environment of the user (let alone the system).
> 
> Regards,
> Arie de Muijnck
> 
> - Original Message - 
> > You might want to try setenv(). Just type 'man 3 setenv' in a Linux 
> > shell to get the man-page for that function (which is in stdlib).
> >
> > Sander
> >
> >
> > -Original Message-
> > Hi,
> >
> > Is there a way to create and export from c an environment 
> variable a 
> > bash script can be aware of? I intend something like PATH, etc.
> >
> > Thanks and Regards,
> >
> > VanVliet
> >
> > Dr. Caroline Van Vliet
> > Adfl Group
> > Advanced DSP Flexible Language Group
> > Via Greto di Cornigliano 6R
> > 16152 - Genova, Italy
> > Tel. +39 010 8609370 - Fax +39 010 8609381
>   
> 
> 
>  
> CONFIDENTIALITY NOTICE - This e-mail transmission, and any 
> documents, files or previous e-mail messages attached to it 
> may contain information that

Re: A way to export from c an environment var

2008-12-19 Thread Andrea Gasparini
Sander Huijsen spiffera, venerdì 19 dicembre 2008 circa:
> I guess there's more than one way to solve a problem. :)
>
> The initial setting of EVR was there just to show that you can also use
> the variable in the C program using getenv(). Your solution works fine
> if there's only one env var to set (apart from some other practical
> problems that may arise). When you want to set multiple env vars, it
> wouldn't work.

Well, in this case you can output the string to be evaluated. Something 
like: 
$ ./your_c_program
EVR1='foo'
EVR2='bar'
And then, you could simply do:
$ `./your_c_program` 
$ echo $EVR1
foo
to evaluate and use your variables.
(IMHO involving files doesn't follow the "keep simple and reusable" Unix 
phylosophy, anyway that discussion begins to become an "high theorical" 
discussion)

my 2cents.
-- 
Andrea Gasparini 
 ImaVis S.r.l. 
web: www.imavis.com

___
Davinci-linux-open-source mailing list
Davinci-linux-open-source@linux.davincidsp.com
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source