helllo every body ..
hope you are having good time programming in unix .
here is a little problem faced me :
setenv("myvar","bla bla",1);
system("myvar=$(grep....));
printf("%s\n", getenv("myvar"));
will print :
bla bla ..
how can i get the value of grep into my program ?
what i figurd out that is after
system("myvar=hello");
myvar will have the same old value,
but:
system("myvar=hello; echo $myvar");
will pring hello without changing myvar value.
The system function runs the command in a subshell (child) and parent shells don't inherit variables of childs.
Regards
what if you invoke your script with a "source scriptname" ?
max_475
4
you mean running
VAR=$( ...)
in seperate script ?
it dosn't work
max_475
5
i thought something like this is happning,
do you think learning more about "fork" will help me?
Use the popen function, this is an example to set a environment variable with a value fetched with a shell command:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
FILE *pipein;
char readbuf[80], Myvar[80];
if ((pipein = popen("pwd", "r")) == NULL)
{
perror("popen");
exit(1);
}
fgets(readbuf, 80, pipein);
close(pipein);
setenv(Myvar, readbuf, 1);
printf("%s", getenv(Myvar));
return(0);
}
Regards
max_475
7
I'm from the ubuntuforum, we used to have a "thanks" buttom to express our appreciation!
Thank you, works like magic.
max_475
8
all right i have another way to solve the same problem, but the solution it self need solution .. : )
it's working with exported variable.
but the the problem that appeared here is kind of weird .. here is what i did
...
system("declare -x myvar= ... " ) ;
the weird result when runing the program is :
declare: not found.
and the same thing happen with all built in command like typeset
i tried this :
..
system ("./sc");
/* where sc is script :
* #/bin/bash
* declare .. */
same result.