How do i transfer the contents of shell variable to a variable used in C program

How do i transfer the contents of shell variable to a variable used in C program

Post by caroli » Tue, 24 Jul 2001 20:04:06



Hi,
  Is it possible to transfer the contents of the shell variable to a
variable used in C program. If so, how can i achieve this.
  Thanks in advance.
caroline.c
 
 
 

How do i transfer the contents of shell variable to a variable used in C program

Post by nos.. » Tue, 24 Jul 2001 21:02:57



>   Is it possible to transfer the contents of the shell variable to a
> variable used in C program. If so, how can i achieve this.  Thanks
> in advance.  caroline.c

The C program could use popen() to run the script, and read its
output, then assign the output of the script to some internal
variable.

 
 
 

How do i transfer the contents of shell variable to a variable used in C program

Post by Uwe Pros » Tue, 24 Jul 2001 21:55:31



> Hi,
>   Is it possible to transfer the contents of the shell variable to a
> variable used in C program. If so, how can i achieve this.

I am not sure what you want. If you want to pass a env-variable to a
c-program you could just pass the variable as a command line argument:

myCprogram $MY_ENV_VAR

If you want to use env-variables inside your c-program you could use the
function

char *getenv(const char *name)

from <stdlib.h>.

Uwe

--

 
 
 

How do i transfer the contents of shell variable to a variable used in C program

Post by Lew Pitch » Tue, 24 Jul 2001 22:20:13



Quote:>Hi,
>  Is it possible to transfer the contents of the shell variable to a
>variable used in C program. If so, how can i achieve this.
>  Thanks in advance.

Use the getenv() call.

For instance,

 # include <stdlib.h>

  {
     char *string;

     if ((*string = getenv("PATH")) != NULL)
       printf("Your PATH environment variable is set to %s\n",string);
     else
       printf("Gasp!! Your PATH is not set!!\n");

  }

Lew Pitcher, Information Technology Consultant, Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employer's.)

 
 
 

1. file contents into variable, or global variable from shell script

I want to maintain a filename across editing sessions (i access my
editor through csh shell scripts).  I have two choices

1) shell script writes filename to file, reads it later.  This requires
a shell command to read first word of a file into a variable - I don't
know how to do this - can you help?

2) shell script defines global variable as filename.  I don't know how
to do this either (the variable goes away when the script completes).

Any suggestions appreciated, please use email

thanks

2. mlock() Problem!!!!

3. shell scripting: echoing contents of contents of variable

4. sed question

5. Expanding a shell variable that may contain shell variables.

6. Compiling C++ code using xlC 3.6.4

7. Setting variables in a variable array of null variables

8. Diamond Stealth II S220 Help! (don't want to give up this easily)

9. Reading HTTP POST Variables in Shell Variables - HOW?

10. Creating shell variable containing substring of another variable.

11. c shell script; Howto use environement variables to build environment variables and take their value

12. Mixing Shell variables and AWK variables

13. ENV Shell Variable to Normal Variable Qn