Thread: Envir. variables into C program

  1. #1
    Registered User
    Join Date
    Feb 2005

    Envir. variables into C program


    I'm fairly new to both linux and C programming so this might be a stupid question.

    I'm trying to write a C program that calls a shell script which does an scp and also sets an environment variable.

    Then I would like to use this variable in my C program. The problem is that I can't get those evironment variables into my C program for some reason. What am I doing wrong?

    Here is my C program:

    #include <stdio.h>
    #include <stdlib.h>
    int main (int argc, char *argv[], char *envp[])
                    system(". getfile");	 	
                    printf("ojeniv var is %s\n",envp[16]);		return 0;
    I know there has got to be a better way to get at my environmental variable "ojeniv" than envp[16] but it's the best I could do for now.

    Here is the script (getfile) that it called:

        scp zephost:/root/signal /home/oj/junk/signal
        ojeniv=`grep seg2 /home/oj/junk/signal | gawk '{print$2}'`
        echo $ojeniv
        export ojeniv=$ojeniv

    It seems like when I run the C program the environment variable will not be set. It will only change if i do a ". getfile" . What's going on here?

    Any help is appreciated


  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    #include <stdio.h>
    int main( int argc, char *argv[], char *env[] )
            int x;
            for( x = 0; env[x]; x++ )
                    printf("%s\n", env[x] );
            return 0;
    Also consider 'man 3 getenv'.

    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Environment variables are inherited only from a parent process to a child process.
    So invoking a child process and then trying to read it's environment after it has quit simply isn't going to work.

    I suggest something like this for your script. Note that the output is sent to stdout, not some variable.
    scp zephost:/root/signal /home/oj/junk/signal
    grep seg2 /home/oj/junk/signal | gawk '{print$2}'
    In your C code, you do this
    int main ( ) {
        char buff[BUFSIZ]
        FILE *fp = popen( "./getfile", "r" );  /* run a process, and read it's stdout */
        while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
            /* buff contains successive lines of the output of the process you invoked */
        pclose( fp );
        return 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Works like a charm Salem.

    I don't see how the output of my script gets into stdout but I'll look into that.

    Thanks a lot,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-07-2009, 09:07 AM
  2. Editing variables while running a flash program.
    By two31d in forum Tech Board
    Replies: 2
    Last Post: 02-28-2006, 01:27 PM
  3. Parse a program for functions, variables
    By Enu in forum C Programming
    Replies: 2
    Last Post: 02-15-2006, 11:08 AM
  4. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 09:01 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM