Professional Web Applications Themes

Output from C program into shell script - UNIX Programming

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 ...

  1. #1

    Default Output from C program into shell script

    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
    Kalpesh Guest

  2. #2

    Default Re: Output from C program into shell script

    com (Kalpesh Parikh) writes:
     

    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
    joe@invalid.address Guest

  3. #3

    Default Re: Output from C program into shell script


    com (Kalpesh Parikh) writes: 

    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: com ******************
    Pascal Guest

  4. #4

    Default Re: Output from C program into shell script

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

    In article <google.com>,
    com says... 


    Your C program prints NOTHING to standard output, whihc is why "my.log"
    file is empty.
    Web Guest

Similar Threads

  1. Execute a shell script from within a C-Program
    By none in forum Linux / Unix Administration
    Replies: 3
    Last Post: July 19th, 09:03 AM
  2. capturing output of a shell comand
    By Betagamma in forum PERL Beginners
    Replies: 1
    Last Post: October 28th, 10:40 AM
  3. Why does my program change the characters in my shell
    By Andrew Falanga in forum UNIX Programming
    Replies: 6
    Last Post: June 28th, 12:17 AM
  4. Execute shell script from a perl script
    By kderaedt in forum Perl / CGI
    Replies: 3
    Last Post: June 26th, 08:26 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139