Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-13-2004, 02:48 PM   #1
LQ Newbie
Registered: May 2004
Posts: 23

Rep: Reputation: 15
Question return value from shell script to c code?


I have a problem with returning a value from shell script to C program. I use system(cmd) to execute a sh file in a C program. How can I get a specific value in the sh and pass it back to my C program? Can it be done by setting environment variable? It seems impossible because parent and child process has it own set of environment variables. Is there any way to accomplish that?

Please help! Thanks!
Old 05-13-2004, 03:43 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
No, you can't set an environment variable in a child process and expect to propagate that change back to the parent process.

And yes, you can absolutely "pass a specific value" from a shell script back to the calling C program. There are *countless* different ways. A few that come immediately to mind are:

1. Use "result = system (cmd)" to read back the return status from the shell program

2. Use "popen (cmd, "r")" instead of "system (cmd)" and then read back whatever
the shell writes to stdout ('popen()" uses pipes to let you read the shell program's
output as though it were a disk file).

3. Have your shell write some output to a data file, then have your C program
open and read the file.

Etc etc etc

It all depends on what your requirements are, and what tools you have at hand.

'Hope that helps .. PSM


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
can a C function return value to Shell Script variable yarnar Programming 17 06-02-2010 05:54 PM
My shell return message always print chaos code backpacker Linux - Newbie 1 06-24-2005 07:05 AM
c code vs shell script again? khucinx Programming 1 05-12-2004 09:44 PM
GNU wget return codes for shell script greenhornet Programming 3 05-09-2004 07:51 PM
return key on shell script chupacabra Programming 2 10-22-2002 12:11 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:11 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration