CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Perl cgi calling shell script

 



dkhanna01
Novice

Aug 5, 2009, 12:38 PM

Post #1 of 23 (4350 views)
Perl cgi calling shell script Can't Post

I have a perl cgi script "cgi1" which I'm calling successfully from apache. I'm making a call to a shell script from cgi1 (my.sh) and it is executing successfully with no issues, but I wanna know there are several "echo" statements written in my shell script,



How can I make all thos echo statements also gets printed in the browser when I call my cgi script


FishMonger
Veteran / Moderator

Aug 5, 2009, 1:21 PM

Post #2 of 23 (4348 views)
Re: [dkhanna01] Perl cgi calling shell script [In reply to] Can't Post

Show us your code so we can show you what needs to be changed.


dkhanna01
Novice

Aug 5, 2009, 1:24 PM

Post #3 of 23 (4345 views)
Re: [dkhanna01] Perl cgi calling shell script [In reply to] Can't Post

I'm sorry but looks like not even my shell script is executing, I'm calling my shell script using system command but looks like it is not getting executed from my cgi- script


FishMonger
Veteran / Moderator

Aug 5, 2009, 1:32 PM

Post #4 of 23 (4344 views)
Re: [dkhanna01] Perl cgi calling shell script [In reply to] Can't Post

In most cases, you'll want to use either a piped open, or the backticks operator to capture the output of the shell script in a var do whatever you want with var.

Have you checked the permission settings on the shell script?

Post your code so we can give you more direct answers.


(This post was edited by FishMonger on Aug 5, 2009, 1:32 PM)


dkhanna01
Novice

Aug 5, 2009, 1:45 PM

Post #5 of 23 (4341 views)
Re: [FishMonger] Perl cgi calling shell script [In reply to] Can't Post

Here is my code:

1 #!/usr/bin/perl
2 use CGI::Carp qw(fatalsToBrowser);
3
4
5 print "Content-type: text/html; charset=iso-8859-1\n\n";
6
7
8 @vlist = `/home/e027449/bin/lockctrl -s -show -br | awk '{print \$1}'`;
9
10 my $q = new CGI;
11
12 my(@FILE) = @vlist;
13 print $q->start_html(-title=>'Select branch');
14
15 print $q->h3({-style=>'Color: red;'},'Select branch from the Drop Down List below...');
16
17 ### start your code ####
18 print $q->start_form();
19 print p($q->popup_menu ("value", \@FILE, 'branch'));
20 print p($q->submit (-name=>'Lock',-value=>'Lock'), $q->submit (-name=>'Status', -value=>'Status'));
21 ### end your code ######
22
23 print $q->end_form(), $q->end_html();
24
25
26
27 my $branch = $q->param("value");
28
29
30 $lock = $q->param('Lock');
31 $unlock = $q->param('UnLock');
32 $state = $q->param('Status');
33 $cron = $q->param('ManualCron');
34
35
36 if ($lock) {
37 print "executing lock\n";
38 my $script = "/home/e027449/bin/lockctrl -s -lock -br $branch -repository stp";
39 $status = system("$script");
40 if (! $status ){
41 print $q->h2 ("$branch Branch is now locked");
42 }
43 else {
44 print "Lock not executing\n";
45 }
46 }
47 elsif ($state) {
48 my $script = "/home/e027449/bin/lockctrl -s -show -br $branch";
49 $status = system($script);
50 }
51 else {
52 print "select something\n";
53 }
54
55 $q->delete_all();
56 print $q->hidden(mode=>'save');
57

Now line no 8 is executing perfactly fine and populating my drop down list, but when I try to hit Lock button somehow its not executing the "if" loop for lock


FishMonger
Veteran / Moderator

Aug 5, 2009, 3:21 PM

Post #6 of 23 (4331 views)
Re: [dkhanna01] Perl cgi calling shell script [In reply to] Can't Post

Please use the code tags when posing your code and don't include the line numbers.

Why do you think the if block is not being executed? Would that be because you don't see any of its output?

Change line 23 from:

Code
print $q->end_form(), $q->end_html();

to:

Code
print $q->end_form();


And add this line after line 56.

Code
print $q->end_html();



dkhanna01
Novice

Aug 5, 2009, 4:01 PM

Post #7 of 23 (4323 views)
Re: [FishMonger] Perl cgi calling shell script [In reply to] Can't Post

when I hist Lock button I don't any of the line executed in if ($lock) loop not even the print function written in there.



Looks like my param assigning the values correctly


FishMonger
Veteran / Moderator

Aug 5, 2009, 4:06 PM

Post #8 of 23 (4322 views)
Re: [dkhanna01] Perl cgi calling shell script [In reply to] Can't Post


In Reply To
when I hist Lock button I don't any of the line executed in if ($lock) loop not even the print function written in there.



Looks like my param assigning the values correctly


Did you make the change I suggested?

Can you post the html code for the form?

Print out the $lock var prior to the if block to see what it holds.


KevinR
Veteran


Aug 5, 2009, 6:33 PM

Post #9 of 23 (4319 views)
Re: [FishMonger] Perl cgi calling shell script [In reply to] Can't Post

I could be wrong, I can only go by what he posted, but his code has never loaded the CGI module so none of the CGI modules methods should even be working.
-------------------------------------------------


(This post was edited by KevinR on Aug 5, 2009, 6:35 PM)


FishMonger
Veteran / Moderator

Aug 5, 2009, 7:02 PM

Post #10 of 23 (4314 views)
Re: [KevinR] Perl cgi calling shell script [In reply to] Can't Post


In Reply To
I could be wrong, I can only go by what he posted, but his code has never loaded the CGI module so none of the CGI modules methods should even be working.


Good eyes, I missed that...it should have given this error in the browser:


Quote
Can't locate object method "new" via package "CGI" ....



dkhanna01
Novice

Aug 6, 2009, 8:07 AM

Post #11 of 23 (4306 views)
Re: [FishMonger] Perl cgi calling shell script [In reply to] Can't Post

First of all thanks for all your replies and helping me out..

Yup I did made those chances you suggested and this is how my code looks like
1 #!/usr/bin/perl
2 use CGI::Carp qw(fatalsToBrowser);
3
4
5 print "Content-type: text/html; charset=iso-8859-1\n\n";
6
7
8 @vlist = `/home/e027449/bin/lockctrl -s -show -br | awk '{print \$1}'`;
9
10 my $q = new CGI;
11
12 my(@FILE) = @vlist;
13 print $q->start_html(-title=>'Select branch');
14
15 print $q->h3({-style=>'Color: red;'},'Select branch from the Drop Down List below...');
16
17 ### start your code ####
18 print $q->start_form();
19 print p($q->popup_menu ("value", \@FILE, 'branch'));
20 print p($q->submit (-name=>'Lock',-value=>'Lock'), $q->submit (-name=>'Status', -value=>'Status'));
21 ### end your code ######
22
23 print $q->end_form();
24
25
26
27 my $branch = $q->param("value");
28
29
30 $lock = $q->param('Lock');
31 $unlock = $q->param('UnLock');
32 $state = $q->param('Status');
33 $cron = $q->param('ManualCron');
34
35
36 if ($lock) {
37 print "executing lock\n";
38 my $script = "/home/e027449/bin/lockctrl -s -lock -br $branch -repository stp";
39 $status = system("$script");
40 if (! $status ){
41 print $q->h2 ("$branch Branch is now locked");
42 }
43 else {
44 print "Lock not executing\n";
45 }
46 }
47 elsif ($state) {
48 my $script = "/home/e027449/bin/lockctrl -s -show -br $branch";
49 $status = system($script);
50 }
51 else {
52 print "select something\n";
53 }
54
55 $q->delete_all();
56 print $q->hidden(mode=>'save');
57 $q->end_html();

THe line no 8 is executing successfully and my drop down list is getting populated with @vlist values successfully, but when I select any of the value from the list its not assigning its value correctly to $branch


KevinR
Veteran


Aug 6, 2009, 8:20 AM

Post #12 of 23 (4303 views)
Re: [dkhanna01] Perl cgi calling shell script [In reply to] Can't Post

Try adding this line to the script:

use CGI;

That way all the CGI modules methods will be available to your program, otherwise it looks like your script should abort as soon as it gets to a line that calls one of the CGI modules methods.
-------------------------------------------------


dkhanna01
Novice

Aug 6, 2009, 8:23 AM

Post #13 of 23 (4302 views)
Re: [KevinR] Perl cgi calling shell script [In reply to] Can't Post

Up I added this as well but no luck :( now I'm like tottaly confused .. where I'm doing wrong


FishMonger
Veteran / Moderator

Aug 6, 2009, 8:46 AM

Post #14 of 23 (4301 views)
Re: [dkhanna01] Perl cgi calling shell script [In reply to] Can't Post

Please use the code tags when posting your code and do not include the line numbers!!


In Reply To
THe line no 8 is executing successfully and my drop down list is getting populated with @vlist values successfully, but when I select any of the value from the list its not assigning its value correctly to $branch


The code you posted doesn't do that, instead it gives this error.

Quote
Can't locate object method "new" via package "CGI" at ... line 10


Once you fix that error, you get this one for lines 19 & 20:

Quote
Undefined subroutine &main::p called


Fix those problems, then add these lines near the top and fix the problems that they point out.

Code
use strict; 
use warnings;



dkhanna01
Novice

Aug 6, 2009, 8:52 AM

Post #15 of 23 (4299 views)
Re: [FishMonger] Perl cgi calling shell script [In reply to] Can't Post

surprizingly its not giving me any error / warnings I even used Strict and removed later after removing all the warnings


FishMonger
Veteran / Moderator

Aug 6, 2009, 9:00 AM

Post #16 of 23 (4298 views)
Re: [dkhanna01] Perl cgi calling shell script [In reply to] Can't Post


In Reply To
surprizingly its not giving me any error / warnings I even used Strict and removed later after removing all the warnings


That tells me that you didn't post the same code that you tested.

DO NOT remove the strict or warnings pragmas. They should be in EVERY Perl script you write.


dkhanna01
Novice

Aug 6, 2009, 9:07 AM

Post #17 of 23 (4296 views)
Re: [FishMonger] Perl cgi calling shell script [In reply to] Can't Post

This is my latest version of the code:

#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use CGI qw(param);
use CGI::Carp qw(fatalsToBrowser);
use CGI qw/:standard/;

use CGI qw(:standard marquee object embed);

use Term::ANSIColor qw(:constants);


print "Content-type: text/html; charset=iso-8859-1\n\n";


my $brlist = `/home/e027449/bin/lockctrl -s -show -br | awk '{print \$1}'`;
my $brlist = "\n".$brlist;
my @vlist = split("\n",$brlist);

my $q = new CGI;

my(@FILE) = @vlist;
print $q->start_html(-title=>'Select branch');

print $q->h3({-style=>'Color: red;'},'Select branch from the Drop Down List below...');

### start your code ####
print $q->start_form();
print $q->popup_menu(-name=>'list',
-id=>'list',
-values=>\@vlist,
-default=>'stp_rel_08_02_copy_only');
print p($q->submit (-name=>'Lock',-value=>'Lock'), $q->submit (-name=>'UnLock',-value=>'UnLock'), $q->submit (-name=>'Status', -value=>'Status'), $q->submit (-name=>'ManualCron',-value=>'ManualCron'));
### end your code ######
print "Selected value: ".$q->param('list');

print $q->end_form();
my $branch = $q->param('list');

my $lock = $q->param('Lock');
print "check 12121: $lock\n";
my $unlock = $q->param('UnLock');
my $state = $q->param('Status');
my $cron = $q->param('ManualCron');

#print "check 22222:$branch\n";

if ($lock) {
print "executing lock\n";
my $status = `/home/e027449/bin/lockctrl -s -lock -br $branch -repository stp`;
#my $status = system("$script");
if (! $status ){
print $q->h2 ("$branch Branch is now locked");
}
else {
print "Lock not executing\n";
}
}
elsif ($unlock) {
print "executing unlock\n";
my $script = "/usr/local/adm/branch_key $branch 'unlock'";
my $status = system($script);
if (! $status ){
print $q->h2 ("$branch Branch is now unlocked");
}
}
elsif ($state) {
print "executing status\n";
my $script = "/home/e027449/bin/lockctrl -s -show -br $branch";
my $status = system($script);
}
elsif ($cron) {
my $script = "/usr/local/adm/email.cronbuild405 '$branch Manual CronBuild' > /tmp/buildlog405.$branch.$$ 2>&1";
my $status = system($script);
if (! $status ){
print $q->h2 ("Manual Cron for $branch Branch is done please check your email for results");
}
}
else {
print "select something\n";
}

$q->delete_all();
print $q->hidden(mode=>'save');
print $q->end_html();

while executing on command line I'm geeting warning messages like:

Use of uninitialized value in concatenation (.) or string at ./testenv line 38
Use of uninitialized value in concatenation (.) or string at ./testenv line 44

Which is:

print "Selected value: ".$q->param('list');
print "check 12121: $lock\n";


KevinR
Veteran


Aug 6, 2009, 9:41 AM

Post #18 of 23 (4289 views)
Re: [dkhanna01] Perl cgi calling shell script [In reply to] Can't Post

See if this helps, I can't test it besides making sure there are no syntax or logic errors, so run it and see if it does what it should on your server:


Code
#!/usr/bin/perl 

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $q = CGI->new;
print $q->header();


my $brlist = `/home/e027449/bin/lockctrl -s -show -br | awk '{print \$1}'`;
my @vlist = split("\n",$brlist);

print $q->start_html(-title=>'Select branch');
print $q->h3({-style=>'Color: red;'},'Select branch from the Drop Down List below...');

### start your code ####
print $q->start_form();
print $q->popup_menu(-name=>'list',
-id=>'list',
-values=>\@vlist,
-default=>'stp_rel_08_02_copy_only');

print $q->submit (-name=>'Lock',-value=>'Lock'),
$q->submit (-name=>'UnLock',-value=>'UnLock'),
$q->submit (-name=>'Status', -value=>'Status'),
$q->submit (-name=>'ManualCron',-value=>'ManualCron');

### end your code ######
print "Selected value: ".$q->param('list');

print $q->end_form();
my $branch = $q->param('list');

my $lock = $q->param('Lock');
my $unlock = $q->param('UnLock');
my $state = $q->param('Status');
my $cron = $q->param('ManualCron');

#print "check 22222:$branch\n";

if ($lock) {
print "executing lock\n";
my $status = `/home/e027449/bin/lockctrl -s -lock -br $branch -repository stp`;
#my $status = system("$script");
if (! $status ){
print $q->h2 ("$branch Branch is now locked");
}
else {
print "Lock not executing\n";
}
}
elsif ($unlock) {
print "executing unlock\n";
my $script = "/usr/local/adm/branch_key $branch 'unlock'";
my $status = system($script);
if (! $status ){
print $q->h2 ("$branch Branch is now unlocked");
}
}
elsif ($state) {
print "executing status\n";
my $script = "/home/e027449/bin/lockctrl -s -show -br $branch";
my $status = system($script);
}
elsif ($cron) {
my $script = "/usr/local/adm/email.cronbuild405 '$branch Manual CronBuild' > /tmp/buildlog405.$branch.$$ 2>&1";
my $status = system($script);
if (! $status ){
print $q->h2 ("Manual Cron for $branch Branch is done please check your email for results");
}
}
else {
print "select something\n";
}

$q->delete_all();
print $q->hidden(mode=>'save');
print $q->end_html();

-------------------------------------------------


KevinR
Veteran


Aug 6, 2009, 9:54 AM

Post #19 of 23 (4287 views)
Re: [dkhanna01] Perl cgi calling shell script [In reply to] Can't Post

dkhanna01,

Just noticed you have been a member of this forum almost as long as I have. Not too many of us old timers left around here anymore. Unsure
-------------------------------------------------


dkhanna01
Novice

Aug 6, 2009, 10:39 AM

Post #20 of 23 (4283 views)
Re: [KevinR] Perl cgi calling shell script [In reply to] Can't Post

I just looked at what you did and I just wanna tell you .... BIG BIG THANKS to you .. and you can hit me on my hands for that..


Just a few simple syntax here and there ...... you'r brilinant..

THANKS MUCH


dkhanna01
Novice

Aug 6, 2009, 10:44 AM

Post #21 of 23 (4282 views)
Re: [dkhanna01] Perl cgi calling shell script [In reply to] Can't Post

now I'm getting all my echo statements print out in my browser when I'm calling shell script.

It's just that it is printing in all straight line and not recognizing line feed from shell script


FishMonger
Veteran / Moderator

Aug 6, 2009, 11:05 AM

Post #22 of 23 (4278 views)
Re: [KevinR] Perl cgi calling shell script [In reply to] Can't Post

Personally, I'd use radio buttons instead of the multiple submit buttons and I'd use a dispatch table.

Here's the modified version with those changes.


Code
#!/usr/bin/perl  

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $q = CGI->new;
my %form = $q->Vars;

print $q->header(),
$q->start_html(-title=>'Select branch'),
$q->h3({-style=>'Color: red;'},'Select branch from the Drop Down List below...');


my @vlist = `/home/e027449/bin/lockctrl -s -show -br | awk '{print \$1}'`;


### start form your code ####
print $q->start_form();
print $q->popup_menu(-name => 'list',
-id => 'list',
-values => \@vlist,
-default => 'stp_rel_08_02_copy_only');

print $q->radio_group(-name => 'action',
-values => ['Lock','UnLock','Status','ManualCron'],
-default => 'Lock');

print $q->p,
$q->submit(-name=>'Proceed',-value=>'Proceed');

print $q->end_form();

### end your form code ######

print $q->br, "Selected value: ", $q->param('list') if $q->param('list');

my %action = (
Lock => sub {
print $q->p("executing lock\n");
my $status = `/home/e027449/bin/lockctrl -s -lock -br $form{'list'} -repository stp`;

if (! $status ){
print $q->h2 ("$form{'list'} Branch is now locked");
}
else {
print "Lock not executing\n";
}
},

UnLock => sub {
print $q->p("executing unlock\n");
my $script = "/usr/local/adm/branch_key $form{'list'} 'unlock'";
my $status = system($script);
if (! $status ){
print $q->h2 ("$form{'list'} Branch is now unlocked");
}
},

Status => sub {
print $q->p("executing status\n");
my $script = "/home/e027449/bin/lockctrl -s -show -br $form{'list'}";
my $status = system($script);
},

ManualCron => sub {
my $script = "/usr/local/adm/email.cronbuild405 '$form{'list'} Manual CronBuild' > /tmp/buildlog405.$form{'list'}.$$ 2>&1";
my $status = system($script);
if (! $status ){
print $q->h2 ("Manual Cron for $form{'list'} Branch is done please check your email for results");
}
},
);

if ( exists $form{'action'} ) {
$action{ $form{'action'} }->();
}

$q->delete_all();
print $q->hidden(mode=>'save');
print $q->end_html();



(This post was edited by FishMonger on Aug 6, 2009, 11:08 AM)


dkhanna01
Novice

Aug 6, 2009, 2:37 PM

Post #23 of 23 (4269 views)
Re: [FishMonger] Perl cgi calling shell script [In reply to] Can't Post

YOU ROCKS

Cool

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives