I am calling a shell script from a c program. I want this script to return multiple values which I want to store in a structure of my c program. For instance say 'a.c' calls 'a.sh', I want 'a.sh' to return the values to be stored in variables 'x' and 'y' which are a part of struct 's' in 'a.c'.
Asked
Active
Viewed 855 times
0
-
1And you've tried what ? – Brian Agnew Aug 20 '13 at 09:36
-
I have no clue as to what to do! – user2430996 Aug 20 '13 at 09:38
-
Read [this answer](http://stackoverflow.com/questions/2488715/idioms-for-returning-multiple-values-in-shell-scripting) Good discussions has already taken place on these topics. – Antarus Aug 20 '13 at 09:45
2 Answers
2
You cannot "return" values from a shell script, other than the single integer (often limited to 8 bits) of exit status that processes have (assuming a Unix-like system, now).
What you typically do is interpret the standard output of the external program, which you can easily read into your C program by using popen()
to run the shell script.
You can transfer any amount of data using this approach.

unwind
- 391,730
- 64
- 469
- 606
1
Not the most elegant solution (but very quick and easy to implement) but you could always produce an output file from the shell script and then parse that file from the C program. This has the benefit that you can store the output values of the shell script between executions of your C program, useful if those output values do not change every time.

James Elderfield
- 2,389
- 1
- 34
- 39