Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 15th, 1999, 05:12 PM
thai
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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") | | 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") | | 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") | | 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") | | 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") | | 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") | | print "COULD NOT OPEN THE script3 FILE<BR>n";
@array = <THEFILE>;
foreach $line (@array) { print "$line";}


Reply With Quote
  #2  
Old July 23rd, 1999, 04:15 AM
jamesh
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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") | |
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > capture and display output on the web

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap