LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2008, 04:10 AM   #1
atrac
LQ Newbie
 
Registered: Oct 2007
Posts: 10

Rep: Reputation: 0
Running a BASH Script within PERL pm


I want to run a bash script within a perl script using perl-cgi

1. User will enter a number in the form page
2. Form will call a URL pointing to a .pm program
3. pm script runs and processes a bash program

I can't get my action page to write a file: /var/www/cgi-bin/date.txt
See below.
Anyone knows how.

1. Form page:

#!/usr/bin/perl
use strict;

use CGI;

my $cgi = new CGI;

print
$cgi->header() .
$cgi->start_html(-title =>'Testing the Form') .
$cgi->h1('Test Forms using PERL!') .
$cgi->startform ('POST', '/cgi-bin/processForm.pm') .
$cgi->textfield(-name=>'username', -default=>'Admin', -size=>'10', -maxlength
=>'10') .
$cgi->submit(-name=>'submit', -value=>'Send') .
$cgi->endform () .
$cgi->end_html;

exit (0);

2. Action page

#!/usr/bin/perl
use strict;

use CGI;

my $cgi = new CGI;

print
$cgi->header() .
$cgi->start_html(-title =>'Form Results') .
$cgi->h1('Form Results using PERL!') ;
my $user = $cgi->param('username');
print "<H4>";
print "$user</H4><H4>\n";
exec("/var/www/cgi-bin/echoDate.sh");
print "</H4>\n";
print $cgi->end_html;

exit (0);

3. Bash program

#/bin/sh
echo $(date "+%d %b %Y") >> /var/www/cgi-bin/date.txt
#touch /var/www/cgi-bin/hello.txt
 
Old 04-11-2008, 09:03 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
There's a much better way to get a date string than shelling out to the date command... See the manual page for the Date::Format module.
 
Old 04-12-2008, 12:07 PM   #3
atrac
LQ Newbie
 
Registered: Oct 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
There's a much better way to get a date string than shelling out to the date command... See the manual page for the Date::Format module.
Thanks for replying. The script above is to test a concept. The actual bash script will contain several commands which may need to write to a log file. If I don't write the part ">> <file>", the date shows in the response page. But if I add that part, I expect the date to be written into the file.
 
Old 04-15-2008, 10:22 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
When you use exec() to launch your bash script, the perl script terminates. If you really want to do it this way (and I can't imagine why you would ever want to drop to a bash script from perl), you can either fork() a child process to launch the shell script, or use the system() perl command, use the `backtick` format of the command, or...

Probably, your cgi-bin directory is [correctly] configured with permissions disallowing writes by the web server. To allow Write-Execute permission would be a security issue. Use a directory with no execute permission.
--- rod.
 
Old 05-10-2008, 12:09 AM   #5
atrac
LQ Newbie
 
Registered: Oct 2007
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
Probably, your cgi-bin directory is [correctly] configured with permissions disallowing writes by the web server. To allow Write-Execute permission would be a security issue. Use a directory with no execute permission.
--- rod.
Thanks theNbomr.

No more BASH in my scripts, just PERL.

You were right, Apache was running as "apache", and the log files in /var/log/* were showing write permission errors.

Getting the script to work is a matter of setting the right permissions.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
why this script is running in bash but not in perl ratul_11 Linux - General 2 08-17-2007 09:31 AM
Help: Perl or Bash Script help embsupafly Programming 1 08-11-2005 06:01 PM
perl/bash script to monitor all processes running in my machine pudhiyavan Linux - Security 4 07-19-2005 02:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration