- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- How to set environment variables using C/C++ throu...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-11-2007 04:37 PM
тАО10-11-2007 04:37 PM
How to set environment variables using C/C++ through PERL script
I want to run a PERL script through my C/C++ code. In the PERL script i am generating a SHELL script that sets some environment variables. As discussed previously in the thread,
But, now my question is can we run this PERL script through the C/C++ code. Because i think it is not possible to run an executable as Source (Please correct me if i am wrong).
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1164715
Please let me know the solution for this.
thanks in advance
Vikram
- Tags:
- environment
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-11-2007 10:55 PM
тАО10-11-2007 10:55 PM
Re: How to set environment variables using C/C++ through PERL script
you're right - this is not possible.
But since you already create a file for the environment variables and theier values:
Just create them in the form
var1=value for it
var2=other value for that
Then in your C-code read this file line-by-line and feed the string into putenv().
Codefragment (no error checking):
FILE *ef = fopen("envfile","r");
char str[128];
while (fgets(str,127,ef) putenv(str);
fclose(ef);
mfG Peter
- Tags:
- putenv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-11-2007 11:43 PM
тАО10-11-2007 11:43 PM
Re: How to set environment variables using C/C++ through PERL script
You can set all the environment variables you like for that process, but they will not be visible to the current process running the C code.
WHY do you think you need to go trhough perl to sh to do this?
WHAT is the value the perl step adds?
WHY not do it all directly in the C program, callign putenv?
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2007 02:37 AM
тАО10-12-2007 02:37 AM
Re: How to set environment variables using C/C++ through PERL script
But, putenv amd getenv will work inside the program. Because i think when we execute a C/C++ program separate process is started then whatever ssettings we do will be there in that Process. So after completion of execution if i do
echo $HELLO
where "Hello=Hai" string is present ia file and this string i am reading from that file.
Is there any way i can achieve this.
This i want because we already have a PERL script that is generating the file containing the environment variable list.
Can any body please help me out of this Problem.
thanks and regards
Vikram
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2007 02:55 AM
тАО10-12-2007 02:55 AM
Re: How to set environment variables using C/C++ through PERL script
This thread (as you noted) is a continued discussion of your previous thead.
> ...we already have a PERL script that is generating the file containing the environment variable list.
Then, in your C/C++ program read the generated file of environmental variables; parse the variable and associated value and issue 'putenv' calls.
Peter, in fact, has already shown a code snippet to do this. His C/C++ suggestion is the same as my shell suggestion in you earlier thread. There isn't any more magic here. Let your Perl script generate a common environmental configuration file for you shell process (in shell syntax) and let your C/C++ program conform to parsing and using that format to apply 'putenv' calls.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-12-2007 03:02 AM
тАО10-12-2007 03:02 AM
Re: How to set environment variables using C/C++ through PERL script
Your Perl script could do something like write to stdout a series of strings "AAA=aaa\n","BBB=bbb\n", .... and if your Perl script were called by popen() for example, it would be able to then call putenv() using each of these strings. That (or a similar scheme using a temporary file) is the only way a child can alter a parent's environment because it is doing it indirectly.