Output from C program into shell script
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Output from C program into shell script




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Output from C program into shell script  
Kalpesh Parikh


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:32 PM

I am executing a "C" program from my shell script.  I am not able to
capture output from my "C" program into the log file name in my
script. Here is an example :


/* ----------------------  a.c program    */
#incldue <stdio.h>
#incldue <sys/file.h>

main()
{

FILE *fp;

if (( fp = fopen ("a.log", "w" )) ! = NULL ){

fprintf(fp, "Hello World \n");
fclose (fp);

}

}

/* End of a.c program */
## then I compiled above a.c program which generated a.out

## Beginning of my.ksh script

#!/bin/ksh

rm -f a.log

./a.out | tee -a my.log

ls a.log my.log

# End of my.ksh script


Note:  a.c and my.ksh files are in same directory.

I see that "Hello World" is printed in a.log file but not in my.log
file.

How do I get "Hello World" printed in my.log file as well ? (Current
my.log file has zero bytes after I ran my.ksh.

Any idea how can I do that ?

Thanks,

Kalpesh





[ Post a follow-up to this message ]



    Re: Output from C program into shell script  
joe@invalid.address


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:32 PM

parikhk@hotmail.com (Kalpesh Parikh) writes:
quote:
> I am executing a "C" program from my shell script. I am not able to > capture output from my "C" program into the log file name in my > script. Here is an example : > > > /* ---------------------- a.c program */ > #incldue <stdio.h> > #incldue <sys/file.h> > > main() > { > > FILE *fp; > > if (( fp = fopen ("a.log", "w" )) ! = NULL ){ > > fprintf(fp, "Hello World \n"); > fclose (fp); > > } > > } > > /* End of a.c program */ > ## then I compiled above a.c program which generated a.out > > ## Beginning of my.ksh script > > #!/bin/ksh > > rm -f a.log > > ./a.out | tee -a my.log > > ls a.log my.log > > # End of my.ksh script > > > Note: a.c and my.ksh files are in same directory. > > I see that "Hello World" is printed in a.log file but not in my.log > file. > > How do I get "Hello World" printed in my.log file as well ? (Current > my.log file has zero bytes after I ran my.ksh.
The tee command takes the standard output of a.out, displays it on the screen, and writes it to the file my.log. The problem is that a.c doesn't generate anything on standard output, it only writes directly to the file. Add a line that calls printf() as well. Joe




[ Post a follow-up to this message ]



    Re: Output from C program into shell script  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:32 PM


parikhk@hotmail.com (Kalpesh Parikh) writes:
quote:
> I see that "Hello World" is printed in a.log file but not in my.log > file.
The shell redirects (or pipes) the "standard output" of your program. This is the file descriptor 1, or the stdio FILE usually named stdout. If you want to read something useful from the shell out of your standard output, you should try to write to stdout instead of to fp. fprintf(stdout,"Hello World\n"); -- __Pascal_Bourguignon__ . * * . * .* . http://www.informatimago.com/ . * . .* There is no worse tyranny than to force * . . /\ () . * a man to pay for what he does not . . / .\ . * . want merely because you think it .*. / * \ . . would be good for him. -- Robert Heinlein . /* o \ . http://www.theadvocates.org/ * '''||''' . SCO Spam-magnet: postmaster@sco.com ******************




[ Post a follow-up to this message ]



    Re: Output from C program into shell script  
Web Surfer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:32 PM

[This followup was posted to comp.unix.programmer]

In article <6b5b649d.0312160928.1fd9c05c@posting.google.com>,
parikhk@hotmail.com says...
quote:
> I am executing a "C" program from my shell script. I am not able to > capture output from my "C" program into the log file name in my > script. Here is an example : > > > /* ---------------------- a.c program */ > #incldue <stdio.h> > #incldue <sys/file.h> > > main() > { > > FILE *fp; > > if (( fp = fopen ("a.log", "w" )) ! = NULL ){ > > fprintf(fp, "Hello World \n"); > fclose (fp); > > } > > } > > /* End of a.c program */ > ## then I compiled above a.c program which generated a.out > > ## Beginning of my.ksh script > > #!/bin/ksh > > rm -f a.log > > ./a.out | tee -a my.log > > ls a.log my.log > > # End of my.ksh script > > > Note: a.c and my.ksh files are in same directory. > > I see that "Hello World" is printed in a.log file but not in my.log > file. > > How do I get "Hello World" printed in my.log file as well ? (Current > my.log file has zero bytes after I ran my.ksh. > > Any idea how can I do that ? > > Thanks,
Your C program prints NOTHING to standard output, whihc is why "my.log" file is empty.




[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:29 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register