【发布时间】:2016-07-15 04:58:01
【问题描述】:
我已经被困了很多天,我有一个 perl 脚本。我在我的 CGI 文件中创建了一个函数,其中包含一些 HTML 内容和一个执行 shell 脚本的系统命令。然后我有一个 href 调用 perl 文件,该文件又调用 CGI 函数并在浏览器上显示内容。但是除了在 CGI 中执行系统命令之外,一切都在加载。我试图将此系统命令放在我的 Perl 文件本身中,并在执行时成功地从命令行执行 shell 脚本。但是我希望这发生在单击按钮时,所以我不得不进行 CGI 调用,但我仍然无法调用它。 我已经经历了很多问题,但我仍在寻找完美的答案。 这是我的代码: test1.pl
#!/usr/bin/perl -w
use strict;
use CGI::Carp qw(fatalsToBrowser);
use Basecamp::UI;
use Basecamp::Dashboard;
use Basecamp::Info;
my $cgi = Basecamp::UI->new->cgi();
my $dashboard = Basecamp::Dashboard->new();
my @html = $cgi->tab_ext();
#push @html, '<div class="page">';
#push @html, newstuff();
#push @html, '</div>';
#print "Hello Hey";
#system( ' sh /home/basecamplocal/Perforce/depot/qeutils/basecamp_dev/sample.sh ');
print $cgi->begin('Basecamp | ext', $dashboard), $cgi->begin_page();
print @html;
print $cgi->end_page(), $cgi->begin_footer(), $cgi->end_footer(), $cgi->end();
shell脚本是:
#!/bin/bash
#perl Basecamp.pl > log_sample.txt
touch log_sample.txt
echo "Good Day"
CGI 文件功能:
=item tab_ext()
======================================================================================
DESC: Generates appropriate tabs for About, about.pl.
OUT: Returns array of scalars with html to draw the tabs and any associated
subtabs.
======================================================================================
=cut
sub tab_ext
{
my ($self) = @_;
my $tab = $self->param('tab');
my @tabs = ('<li><a href="test1.pl?tab=New">Status Restart</a></li>');
my @subtabs = ();
my @html = ();
$tabs[0] = '<li class="selected"><a href="#">Status Restart</a></li>';
@html = ('<ul id="tabs">', @tabs, '</ul>');
push @html, ('<ul id="subtabs">', @subtabs, '</ul>');
print "Content-type: text/html\n\n";
system("sh /full path to shell file /sample.sh ");
return @html;
}
我在 href 调用 html 上调用 test1.pl 文件。除了 shell 脚本的执行之外,正在显示来自 CGI 的整个 html 内容。 我们将不胜感激。
【问题讨论】:
-
请具体问题。该链接是关于系统命令的,我告诉过你它在独立执行时运行良好。
-
无需显式调用
sh(作为CGI脚本执行时,默认$PATH可能不在)。使sample.sh可执行,你可以使用`system('/full/path/to/sample.sh');确保不要像现在这样包含尾随空格。