Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
how to obtain shell variable value in C program? For Example: #cat test.sh read re echo $re I want to get above shell variable's value in a C program... main(){ ...
  1. #1
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Question How to get shell variable in C program?

    how to obtain shell variable value in C program?

    For Example:

    #cat test.sh
    read re
    echo $re
    I want to get above shell variable's value in a C program...

    main(){
    ..........
    ..........

    //printf("%s",mesg); //get the value from shell variable.

    }
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  2. #2
    Just Joined!
    Join Date
    Feb 2008
    Posts
    50
    I think you can access this variable using "getenv". But for this you must have to export this variable so other shell can also access this variable.

    VarValue=getenv(re);
    printf("VarValue=%s\n",VarValue);

  3. #3
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation

    I checked this method last night ....but its not working...

    re=1
    echo $rere

    export rere
    shell variable is exported ....

    I re-checked it using env command...(I can see re=1)
    but when i to use it in C program...something like

    main(char argc,char *argv[],char ** environ)
    {
    char re[25];
    ::::


    printf("%s",getenv(re));
    }
    It displays :

    (null) value

    From getenv i found ,if environment value is not set it will display null..... but i can see the value using env
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You're not doing it quite right. Run this shell script as a corrected example.
    Code:
    #!/bin/bash
    
    re=1
    echo $re
    export re
    
    cat > wednesday.c <<EOD; cc -Wall wednesday.c -o wednesday; ./wednesday
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
      printf("%s\n",getenv("re"));
      return 0;
    } /* main() */
    EOD
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    #!/bin/bash

    re=1
    echo $re
    export re

    cat > wednesday.c <<EOD; cc -Wall wednesday.c -o wednesday; ./wednesday
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
    printf("%s\n",getenv("re"));
    return 0;
    } /* main() */
    EOD
    Sorry for delay reply,no ...it's not working for me..
    This is what i tried -around way method.

    I store these values in a file and then use C program to read this value..


    test.sh
    echo $re >> /tmp/config_file

    test.c - C program

    main (){
    int choice;
    char a;
    --
    --
    fp=open (/tmp/config_file,0) //open temp file
    read(fp,a,1); //read value
    close(fp)
    if(a == '1')
    choice=1;
    else
    choice=0;
    ::::::
    ::::::
    }
    Note both C and Shell are independent programs...When i tried to use getenv()

    is still returns value as
    (null)
    Any idea.....
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    no ...it's not working for me..
    Now I'm really confused. Please be patient with me, and let's take this one step at a time. So, when I run this bash script:
    Code:
    #!/bin/bash
    
    re=1
    echo $re
    export re
    
    cat > wednesday.c <<EOD; cc -Wall wednesday.c -o wednesday; ./wednesday
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
      printf("%s\n",getenv("re"));
      return 0;
    } /* main() */
    EOD
    I get this output:
    Code:
    1
    1
    What output do you get?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Just Joined!
    Join Date
    Feb 2008
    Posts
    50
    Quote Originally Posted by Lakshmipathi View Post
    I store these values in a file and then use C program to read this value..

    i think you are missing allocating space for buffer "a"

    see this :

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>

    int choice;
    char *a;

    int main()
    {
    int fp, size;
    a= malloc(10);

    fp=open("/tmp/config", O_RDONLY);
    printf("open returns: %d\n",fp);
    size = read(fp, a, 1);
    printf("read returns: %d\n",size);
    close(fp);

    printf("value of a: %s\n",a);

    return 0;
    }

    $ ./readf.o
    open returns: 3
    read returns: 1
    value of a: 1

  8. #8
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Question

    Quote Originally Posted by wje_lf View Post
    Now I'm really confused. Please be patient with me, and let's take this one step at a time.
    Quote Originally Posted by wje_lf View Post
    What output do you get?
    I got an error message on cc -wall

    Thanks wje_lf and ypankaj...

    Please be patient with me too.
    I'll explaning what i'm trying : I'm implemeting a GUI interface for my C project.
    Since it's nearly 4k lines of c program. I used a tool called zenity.
    Zenity provides gui interface to shell script.

    My c program can be involved options like
    option 1:
    option 2:
    Enter your choice :
    In C program , If the variable choice = 1 then do this else do that.


    As i'm using zenity,I can easily create a gui with these two options in script program.
    ans=$(zenity --list --text "Chose your option?" --radiolist --column "Pick" --column "Opinion" TRUE Option 1 FALSE Option 2);
    echo $ans
    It will store the value chosen/clicked by user.
    For more about zenity check here A complete zenity dialog examples 2 Linux by Examples

    If i can some how export this shell vairable $ans value to C program.
    In simple terms,I want to assign value of shell variable ans to C program variable choice

    I hope Its clear now...
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  9. #9
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    In simple terms,I want to assign value of shell variable ans to C program variable choice
    The first part of that is to get access to the shell variable. My little sample shell script shows how. But that didn't work for you, because:
    I got an error message on cc -wall
    Yes, because -wall is meaningless, but -Wall means something. Use upper case W, not lower case w. Rather than typing the script by hand, maybe can you cut and paste it into a terminal window or something, and try again? :)

    If it then doesn't work for you, let's find out why. If it does work for you, perhaps it will illustrate how a C program can access an environment variable. (A bash variable becomes an environment variable when one exports it.)

    But ypankaj is correct when he says you have a problem with the C variable a. You declare it as a single char; he, in his revision of your program, declares it as a pointer to (zero or more) chars.

    That pointer doesn't yet point to any actual buffer space; he fixes that by calling malloc(). Please do this at the command prompt:
    Code:
    man malloc
    He makes two errors, though:
    1. Whenever you call malloc(), you should make sure that the resultant pointer is not NULL before continuing. If it's NULL, you should exit with an error message. In this case, that error shouldn't happen, but you should always check for it.
    2. We have no way of knowing that there will be only 9 characters in the string. (The 10th one will be the NUL character which indicates the end of the string.) So instead of doing malloc(), you should be doing strdup(), which will automatically allocate sufficient memory to hold the string. (But don't for get to check that for a returned NULL pointer, either.)

      For more information, please do this at the command line:
      Code:
      man strdup

    Please do not be offended by the following observation. If you declared a to be a single character, perhaps some additional reading on how to program in C could save you a lot of debugging time in the long run. I'd recommend that you scroogle the following search terms:
    Code:
    C tutorial
    and find the tutorial that suits you best.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  10. #10
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    read(fp,a,1); //read value
    I missed & in above stmt .... it should be like this:
    read(fp,&a,1); //read value
    That's what i learned from my teacher....

    I need to re-fresh me C skills again instead of working on things like (php,drupal,j2ee ) ...thanks for the links i'll look into them....
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

Page 1 of 2 1 2 LastLast

Posting Permissions

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