Unix Programming - Output from C program into shell script

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > Output from C program into shell script





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Output from C program into shell script
Kalpesh Parikh

2004-01-23, 5: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
joe@invalid.address

2004-01-23, 5: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
Pascal Bourguignon

2004-01-23, 5: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 ******************
Web Surfer

2004-01-23, 5: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.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com