Find the answer to your Linux question:
Results 1 to 4 of 4
What is the way to get data from shell variables into a GTK+ 2.0 program? If I have a shell variable "svar1" and an environment variable "ENV1", what is the ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    39

    Get data from shell variable into GTK+ 2.0

    What is the way to get data from shell variables into a GTK+ 2.0 program?

    If I have a shell variable "svar1" and an environment variable "ENV1", what is the way to get the value of "svar1" into a GTK+ 2.0 program variable "int i1" and to get the string of "ENV1" into "char buf[ ]"?

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    Quote Originally Posted by felPmy View Post
    What is the way to get data from shell variables into a GTK+ 2.0 program?

    If I have a shell variable "svar1" and an environment variable "ENV1", what is the way to get the value of "svar1" into a GTK+ 2.0 program variable "int i1" and to get the string of "ENV1" into "char buf[ ]"?
    You can pick up environmental variables in any C program by using the getenv() function. You give it the name of the variable as an argument and it returns a pointer to the value. if I were you, I'd use that pointer for subsequent access, not put it in a fixed-length buffer.

    Shell variables other than environmental ones only have value inside the script that defined them. Are you going to launch your program from inside a script? If so, you can pass $svar1 to it as an argument.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    39
    Are you going to launch your program from inside a script?
    No, I had not planned on doing that. With the program name being "program" and inside the /bin/bash shell:

    > svar1=10.
    > echo $svar1
    10
    > program
    < how can "program" put the "10" into the "int i"?

    getenv() function.
    Thx.

  4. #4
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    Presumably you want to define svar1 earlier in the session, then reference it later on in your program. I can think of two ways to do it. One is to export the variable to your environment and have the program reference it via getenv. The other is to pass it as a command-line argument
    Code:
    program $svar1
    and then have something like:
    Code:
    integer_var = atoi(argv[1]);
    inside your program.

    I'd be interested to know if anyone else can give us a third method.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...