[comp.unix.questions] Interfacing C Code to Shell Script

rose@baby.swmed.utexas.edu (Rose Oguz) (05/30/90)

Hello to Everyone!

	I apologize if the question I ask has been asked before; I'm new 
and probably missed the posting.

	I want to call a shell script from my C program.  However, I 
would like the script to return values to my C program.  Is there a
way to do this?  Or must I write the shell output to a file and read the
contents of the file in my C code?  Any ideas, suggestions, hints, etc.
will be greatly appreciated.

					Thanks,
					Rose Oguz
					rose@baby.swmed.utexas.edu

subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (05/31/90)

In article <23487@adm.BRL.MIL> rose@baby.swmed.utexas.edu (Rose Oguz) writes:
>	I want to call a shell script from my C program.  However, I 
>would like the script to return values to my C program.  Is there a

Okay gang, even though many "official" unix.hackers out there might disagree,
this one simply says that popen() is screaming for this job, just as it's
supposed to :-).

In your C shell script, do this:
#!/bin/csh
...
echo "(whatever the status you want to give to the c program)"

then -- in your C program do this:
FILE *cpipe = popen("name_of_cshell_script","r");
then just read from cpipe as if it were a file.
e.g, 
c = getc(cpipe); 
fgets(foo, 500, cpipe);
etc...


			-Kartik

-- 
subbarao@{phoenix,bogey or gauguin}.Princeton.EDU -|Internet
kartik@silvertone.Princeton.EDU (NeXT mail)       -|	
subbarao@pucc.Princeton.EDU		          - Bitnet

ag@cbmvax.commodore.com (Keith Gabryelski) (05/31/90)

In article <16870@phoenix.Princeton.EDU> subbarao@phoenix.Princeton.EDU
(Kartik Subbarao) writes:
>In article <23487@adm.BRL.MIL> rose@baby.swmed.utexas.edu (Rose Oguz) writes:
>>	I want to call a shell script from my C program.  However, I 
>>would like the script to return values to my C program.  Is there a
>
>Okay gang, even though many "official" unix.hackers out there might disagree,
>this one simply says that popen() is screaming for this job, just as it's
>supposed to :-).
>
>[lots of code deleted]

or just:

	int retval = system("myshellscript");

Pax, Keith

finn@eleazar.dartmouth.edu (Andy Behrens) (05/31/90)

rose@baby.swmed.utexas.edu (Rose Oguz) writes:
> 	I want to call a shell script from my C program.  However, I 
> would like the script to return values to my C program.  Is there a
> way to do this?  Or must I write the shell output to a file and read the
> contents of the file in my C code?  

If you need to return a lot of information, then popen(3) is just the
thing (as another poster has suggested).

However, sometimes you only need to return a small integer, perhaps a
success/failure code.  If that's the case, it's easier to call system(3),
and use the shell command

	exit N

to return your status.  

pfalstad@phoenix.Princeton.EDU (Paul John Falstad) (05/31/90)

In article <11985@cbmvax.commodore.com> ag@cbmvax (Keith Gabryelski) writes:
>In article <16870@phoenix.Princeton.EDU> subbarao@phoenix.Princeton.EDU
>(Kartik Subbarao) writes:
>>In article <23487@adm.BRL.MIL> rose@baby.swmed.utexas.edu (Rose Oguz) writes:
>>>	I want to call a shell script from my C program.  However, I 
>>>would like the script to return values to my C program.  Is there a
                                        ^
>>this one simply says that popen() is screaming for this job, just as it's
>or just:
>
>	int retval = system("myshellscript");

This only works for one integer return value that is < 128, of course.
If you need more than that you'll have to do a popen.

Also, for a command as simple as "myshellscript," I would have used
fork, execve, and wait, to save on the overhead of sh.  Even instead of
popen it might be a good idea to try fork, execve, and pipe...
Just a thought.

-- 
Paul Falstad  PLINK:Hypnos GEnie:P.FALSTAD net:pfalstad@phoenix.princeton.edu
Disclaimer: My opinions, which belong to me and which I own, are mine.
-Anne Elk (not AN elk!)  The sun never set on the British empire because
the British empire was in the East and the sun sets in the West.