Difference between revisions of "Interacting with the enviroment"

From Organic Design wiki
m
Line 7: Line 7:
 
This will create an [[w:Asynchronous|asynchronous]] connection to the bash process.
 
This will create an [[w:Asynchronous|asynchronous]] connection to the bash process.
  
=C=
+
==Enviroment variables==
 
It's quite simple to read and write environment variables from C:
 
It's quite simple to read and write environment variables from C:
 
<pre>
 
<pre>
Line 43: Line 43:
 
FOO=bar
 
FOO=bar
 
</pre>
 
</pre>
 +
 +
=Pipes=
 +
 +
[[+Pipe.c]]

Revision as of 22:05, 20 March 2007

bash

To connect to a bash shell you can simply do:

cat > bash

This will create an asynchronous connection to the bash process.

Enviroment variables

It's quite simple to read and write environment variables from C:

char *envvar;

if(envvar = getenv("FOO"))
	printf( "FOO=%s\n", envvar );

putenv("FOO=bar");

When you call a child process a copy of the enviroment is passed to this process. However, if the process changes enviroment variables, these changes will be lost once the process terminates.

The parent C program:

putenv("FOO=bar");

system("sh printenv");

if(envvar = getenv("FOO"))
	printf( "FOO=%s\n", envvar );

Executes a shell script (printenv) with system()

echo "printenv: $FOO"
FOO=bar2

The resulting output is:

printenv: bar
FOO=bar

Pipes

+Pipe.c