#1
  1. No Profile Picture
    thai
    Guest
    Devshed Newbie (0 - 499 posts)
    Hi,

    I'm trying to capture some output from executing a command and then print this output. The problem is that executing this perl script on the command line works
    but it doesn't work from the web. I tried a whole bunch of ways. Could someone clue me in on how to make this thing work? Does it have something to do with having permission to create the files?:

    Thanks.

    (cmd.pl just prints out hello world)
    print "####################################################<BR>n";
    print "Attempt 1: Capture info using pipe command<BR>n";
    open(PIPE, "cmd.pl >script |");
    while (<PIPE> ) {
    push @stuff, $_;
    }
    close (PIPE);

    while (@stuff) {
    $it = shift(@stuff);
    print "$it";
    }

    print "Results of pipe command should be in the file...so check file<BR>n";
    open(THEFILE, "script") &#0124; &#0124; print "Could not open script<BR>n";
    @stuff = <THEFILE>;
    foreach $line (@stuff) {
    print "$linen";
    }
    close(THEFILE);
    print "####################################################<BR>n";
    print "Attempt 2: Capture info using backticks<BR>n";
    @stuff = `cmd.pl`;
    open(MYFILE, ">script1") &#0124; &#0124; print "Could not open script1 for writing<BR>n";
    foreach $line (@stuff) {
    print MYFILE "$line";
    }
    close(MYFILE);
    print "Results of backticks command should be in the file...so check file<BR>n";
    open(MYFILE, "script1") &#0124; &#0124; print "Could not open script1 for reading<BR>n";
    @array = <MYFILE>;
    foreach $line (@array) { print "$line";} close(MYFILE);

    print "####################################################<BR>n";
    print "Attempt 3: Capture info using pipe(w/out '> script' this time)<BR>n";
    open(SYSCALL, "cmd.pl |");
    while (<SYSCALL> ) {
    push @output, $_;
    }
    close (SYSCALL);

    print "Open the file to write the info to...<BR>n";
    open(MYFILE, ">script2") &#0124; &#0124; print "Could not open script2 for writing<BR>n";
    while (@output) {
    $oneline = shift(@output);
    print "$oneline";
    print MYFILE "$oneline";
    }
    close(MYFILE);
    print "Results of pipe command should be in the file...so check file<BR>n";
    open(MYFILE, "script2") &#0124; &#0124; print "Could not open script2 for reading<BR>n";
    @array = <MYFILE>;
    foreach $line (@array) { print "$line";} close(MYFILE);

    print "####################################################<BR>n";
    print "Attempt 4: Capture info using system command<BR>n";
    system("cmd.pl > script3");

    print "Results of system command should be in the file...so check file<BR>n";
    open(THEFILE, "script3") &#0124; &#0124; print "COULD NOT OPEN THE script3 FILE<BR>n";
    @array = <THEFILE>;
    foreach $line (@array) { print "$line";}

  2. #2
  3. No Profile Picture
    jamesh
    Guest
    Devshed Newbie (0 - 499 posts)
    What you want to ensure is that the file is being written to a world writable directory.When the file is written, it will be owned by the web server user (typically 'nobody').


    Try ...
    open(MY_TEMP_FILE, ">/tmp/temp.out") &#0124; &#0124;
    die "Can't open file for writing; $!n";

    You may also need to give the browser an idea of what it is expected to do by sending it a content header before any other output. You can do this simply by 'print "Content-type: text/htmlnn";', or by using the perl module CGI call 'header'. The two newline statements are important, without them the script will fail.

    The system call that you are using may also have to be fully qualified, depending on your system set-up.

    #!/usr/bin/perl

    # listdir.pl
    # prints the local directory

    # get the local directory
    my $directory = `ls`;

    # the directory string will contain newlines.
    # use this convenience to split the string into array elements
    my @file_array = split("n", $directory);

    # print a header
    # using a simple approach here as we don't need CGI
    # features for a simple script (nor overhead).
    print "Content-type: text/htmlnn";

    # recurse through the array and print each array element to
    # STDOUT, in this case, a web page
    foreach $file (@file_array) {
    print "<P>$file</P>n";
    }

    Dont forget to chmod +x the script first.

    or, using shell

    #!/bin/sh
    echo "Content-type: text/html"
    ls

    James

IMN logo majestic logo threadwatch logo seochat tools logo