Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2004
    Location
    South West PA
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    capturing program output

    can you capture a programs output and display it on a web page in php?

    Just today I saw that

    <?php
    $output = shell_exec('who -b | cut -c25-');
    echo "Server's last reboot: $output";
    ?>

    would output the servers last reboot time, by executing the command in ()'s.
    (was looking at my admin's .profile for how he got unix to spit out all the Hi blah blah, its blah blah time stuff when I logged in)

    I thought this was super slick since I had been trying to do this for awhile in CGI/Perl.

    Since the thing I want to do is a compiled program (using gcc to compile a .c file) would I have to pipe its output to a file and then use the shell_exec to print the contents of the file?

    Also, is the php manual good for learning from, or a good resource?

    The one book I have that mentions php only has one chapter, and Im too poor to buy any more books this semester

    I guess I could go through my directories and remake everything in php for starters.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    your executable should just print to stdout , no need to save to file unless you have that specific requirement.

    exec() basically returns stdout from whatever it is executing, if the output is multiline then PHP loads it as an array..

    PHP Code:
    <?
    exec
    ('du -h' $yaks);
    print_r($yaks);
    ?>
    executables that require further input from the user (in this case PHP) are another matter , you can use proc_open to `talk` to an executable but its easier said than done

    if an exec hangs or returns nothing (when you expected it to) redirect stderr to stdout
    <?exec('some_command -switch args &2>1',$yaks);?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Oct 2004
    Location
    South West PA
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First, thanks for the quick reply.

    I tried your code substituting my program name and variable name.

    and my HTML output was
    Array()
    I looked at the manual page for print_r, and im confused as you how that would handle say a paragraph of text.

    would it be an array of strings? where each index would be a line ending in a \n ?

    so the array would be

    1 => Hello\n
    2 => The sky is blue\n
    3 => and cows go moo\n

    Would I get the blank array output because this program is not from the admin ?

    The program is just on my directory (same as my site, and is chmod to 755).

    t

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    chmod to 0755 should be enough for PHP to exec the file.

    As far as I know PHP exec() simply turns output into an array based on newlines & print_r() is just an quick way to view that data.

    did you try adding &2>1 to the end of the command string ?
    <?$command = "/path/to/binary $args &2>1";?>

    also note that exec() and shell_exec() are different

    $output = shell_exec($command);
    exec($command,$output);
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New Coder
    Join Date
    Oct 2004
    Location
    South West PA
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Played around with it some and it works with shell_exec


    <?php
    $result = shell_exec('[filepath]/[program_name]');
    echo $result;
    exit(0);
    ?>


    I can't believe how all these neat things I want to do are only 3-5 lines of code each.

    Thanks for your guidance

  • #6
    New Coder
    Join Date
    Oct 2004
    Location
    South West PA
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well I shouldn't say it works, because my text comes out all wonky. The \n's in the program do not carry over so everything is smooshed.

    I've tried concatenating the result with line breaks but it prints all the output at once then concat's whatever I put.

  • #7
    New Coder
    Join Date
    Oct 2004
    Location
    South West PA
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    fixed it

    went back into my C program abd out <br /> at the end of each line and recompiled.

    Now my whole program executes and prints to the html with the line breaks included.


  •  

    Posting Permissions

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