Find the answer to your Linux question:
Results 1 to 9 of 9
Hi i included shell script inside c program, and i wanted to assign the value of c variable to shell variable..Can any one please suggest me how to do it?? ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    10

    how to assign value of c variable to shell variable?

    Hi i included shell script inside c program, and i wanted to assign the value of c variable to shell variable..Can any one please suggest me how to do it??

    Thanx in advance....

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Quote Originally Posted by vpradeep View Post
    Hi i included shell script inside c program, and i wanted to assign the value of c variable to shell variable..Can any one please suggest me how to do it??

    Thanx in advance....
    Depends...How are you including a shell script inside a C program?
    Make mine Arch Linux

  3. #3
    Just Joined! sixdrift's Avatar
    Join Date
    Jan 2007
    Location
    In and around and about Cary, NC
    Posts
    44
    To do something like this, you must have something dynamic about your script. Either you are generating the script on the fly via printf() or something, and then saving that and executing it, or you have to pass parameters to the script when you execute it.

    So what are you doing?

  4. #4
    Just Joined!
    Join Date
    Jan 2010
    Posts
    10
    i am actually writing shell script into some .sh file using fprintf function and then i am executing the script file with system("sh script.sh") .....it would be good for me if am able to assign shell variable value to c variable also....

  5. #5
    Just Joined!
    Join Date
    Aug 2006
    Location
    North Dakota, USA
    Posts
    14
    Using environment variables in C might be the way to go (though it has been a while since I've worked in C). If the C program can create an environment variable, then the shell program should be able to read it.

    More information on what you are trying to accomplish (as previously stated) would be greatly helpful. I see no reason to downgrade from a C program to use a far less powerful scripting language like shell (i.e. bash, sh, etc).

  6. #6
    khf
    khf is offline
    Just Joined!
    Join Date
    Mar 2009
    Location
    Moves between London, Oslom Brussels
    Posts
    30
    Quote Originally Posted by gerard4143 View Post
    Depends...How are you including a shell script inside a C program?
    Remember that a C program has 2 parameters: argc,argv
    As in "main (int argc, char *argv[]);"

    You pass values to the C program from the command line, and the exit value is the value returned.

    Save "AddItUp.c"
    main(int argc, agrv)
    int argc; char * argv[];
    {
    int total =0;
    while agrc-- total += atoi(argc);
    exit(total);
    }

    Compile this and make AddItUp executable:

    Now the shell script:
    Sum = AddItUp 1 2 3 4 5
    Echo Sum

    Will print 15.

    Strings are more difficult to assign, so most shell scripts use Integer values and you have to make the program that print the text on the correct stream.

    Please bear with me on simple errors - I think I have all the semi-colons, but the shell script - "Bash" is not my favourite.. but you should get the idea.

  7. #7
    khf
    khf is offline
    Just Joined!
    Join Date
    Mar 2009
    Location
    Moves between London, Oslom Brussels
    Posts
    30
    Quote Originally Posted by slaughter View Post
    Using environment variables in C might be the way to go (though it has been a while since I've worked in C). If the C program can create an environment variable, then the shell program should be able to read it.

    More information on what you are trying to accomplish (as previously stated) would be greatly helpful. I see no reason to downgrade from a C program to use a far less powerful scripting language like shell (i.e. bash, sh, etc).
    Sorry - the "setenv()" and "getenv()" works in the current shell - not in a forked shell, where it will "die" with the shell. You need the C program to be in the same shell as you execute. Just as in the other example, you can spin off a new shell, "exec()" and read the int value it returns. But any shell variables are lost when the script terminates - unless the shell script sets global environment variables.

    This used to be real neat in csh in Unix, and Bourne really messed it up (Bash is variant of this). BUT you are investigating some of the most powerful features of Unix and Linux, since provides a huge flexibility. Just remember to document, and keep the C programs small.

  8. #8
    Just Joined! sixdrift's Avatar
    Join Date
    Jan 2007
    Location
    In and around and about Cary, NC
    Posts
    44
    So if you are wanting to obtain some variable value in the C program, call it "maxspeed" or "numbarrels" or whatever, and you want to use that variable in the shell script, you can do a couple things. First, since you are generating the script on the fly, you can either just

    Code:
    fprintf(file,"MAXSPEED=%d\nNUMBARRELS=%d\n", maxspeed, numbarrels);
    or write the script to take command line parameters and just pass them to it. I prefer command line parameters where it makes sense because it is the most flexible. You may not always be writing the script on the fly, and command line parameters work with any number of commands you system().

    Now for getting values out of the script, I have done several things in the past, but the one that always seems to work is to generate output values into a specially formatted output file setup in /tmp with unique name (known by the C program calling the script). Then after the script runs, the C program parses the output file to retrieve variable values.

    There may be more elegant ways, but when you have a lot of output data to handle, or exotic data to handle (like XML output) that works really well.

  9. #9
    khf
    khf is offline
    Just Joined!
    Join Date
    Mar 2009
    Location
    Moves between London, Oslom Brussels
    Posts
    30
    Quote Originally Posted by sixdrift View Post
    So if you are wanting to obtain some variable value in the C program,...

    Code:
    fprintf(file,"MAXSPEED=%d\nNUMBARRELS=%d\n", maxspeed, numbarrels);
    or write the script to take command line parameters and just pass them to it. I prefer command line parameters where it makes sense because it is the most flexible. ....
    Whoops: Not so fast!
    Remember you run in a command shell: "Bash". Search the manual pages for the "environment" functions. If you look at industrial code such as Oracle, you will find that they have a number of documented, and more undocumented environment variables that is read. These have to be in the shell that you execute in, or the parent/login shell.
    When you make a "system()" - this is really a "newpid= fork(); exec(newpic, command)" - and the new process runs in a subshell of current and inherits only the environment from the parent shell. So you can set variables, and then when you execute the new process, this runs with the "old" values. So, you need to set the "parent shell's" environment to make the new process take the new values.

    The alternative is to dump it all to a /tmp file, create this, set variables that the "Bash-shell" sets and use (as the fprintf() above) - and you C program reads the termination status.

    Bear in mind that this is not MS-DOS. Here you can create what you want and get the job done.

Posting Permissions

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