You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Kairam, Raj" <Ka...@coned.com> on 2001/11/30 21:08:16 UTC
Running a shell command inside a cgi/perl script
In my perl script I have a line like this.
system( "'/usr/bin/lp -dhp4si /tmp/plotreq.txt' > /tmp/plotid.txt");
hp4si is the destination printer.
/tmp/plotreq.txt is small text file to be sent to the printer.
/tmp/plotid.txt is the output of lp command ( just one line to indicate job
id )to be saved.
If I run the command /usr/bin/lp -dhp4si /tmp/plotreq.txt > /tmp/plotid.txt
it is fine as a command line.
The same in the perl script as above, doesn't send the file to printer nor
does it create the /tmp/plotid.txt file.
I have tried this also and did not work.
@lplist = ("/usr/bin/lp", "-dhp4si /tmp/plotreq.txt");
system(@lplist);
This is the context.
sub search {
some code
....
open(REQFILE, ">/tmp/plotreq.txt") || die "sorry, could not open
/tmp/plotreq.txt";
some more code to generate content for plotreq.txt
......
close(REQFILE);
.... This is where I tried the above to send the file to the printer.
}
This sub is part of a cgi script that creates output for the browser. The
browser output is OK.
The /tmp/plotreq.txt is generated but not being sent to the printer.
Can somebody guide me to do it right ?.
Thanks
Raj kairam
Re: Running a shell command inside a cgi/perl script
Posted by Rodney Broom <mo...@rbroom.com>.
From: Kairam, Raj <Ka...@coned.com>
> system( "'/usr/bin/lp -dhp4si /tmp/plotreq.txt' > /tmp/plotid.txt");
> If I run the command /usr/bin/lp -dhp4si /tmp/plotreq.txt > /tmp/plotid.txt
> it is fine as a command line.
I may be missing something, but it looks to me like you are running a different command from system() than what you are doing on the command line. That is, system() apears to be getting this:
'/usr/bin/lp -dhp4si /tmp/plotreq.txt' > /tmp/plotid.txt
And the shell apears to be getting this:
/usr/bin/lp -dhp4si /tmp/plotreq.txt > /tmp/plotid.txt
When I need error checking, I'll often use open() instead of system().
---
Rodney Broom
Re: Running a shell command inside a cgi/perl script
Posted by Balazs Rauznitz <ba...@commissioner.com>.
On Fri, Nov 30, 2001 at 03:08:16PM -0500, Kairam, Raj wrote:
> In my perl script I have a line like this.
> system( "'/usr/bin/lp -dhp4si /tmp/plotreq.txt' > /tmp/plotid.txt");
>
> hp4si is the destination printer.
> /tmp/plotreq.txt is small text file to be sent to the printer.
> /tmp/plotid.txt is the output of lp command ( just one line to indicate job
> id )to be saved.
>
> If I run the command /usr/bin/lp -dhp4si /tmp/plotreq.txt > /tmp/plotid.txt
> it is fine as a command line.
>
> The same in the perl script as above, doesn't send the file to printer nor
> does it create the /tmp/plotid.txt file.
>
> I have tried this also and did not work.
> @lplist = ("/usr/bin/lp", "-dhp4si /tmp/plotreq.txt");
You're using system incorrectly; this should work:
system "/usr/bin/lp -dhp4si /tmp/plotreq.txt > /tmp/plotid.txt"; # no singlequotes arounf the command
-Balazs
Re: Running a shell command inside a cgi/perl script
Posted by Brian Reichert <re...@numachi.com>.
Just to toot my own horn, I'd like to mention that I wrote the
System2 module, with an eye toward running commands, and getting
isolated STDOUT/STDERR as well as exit values. Well, it makes me
happy.
On Fri, Nov 30, 2001 at 03:45:23PM -0500, J. J. Horner wrote:
> * Kairam, Raj (KairamR@coned.com) [011130 15:10]:
> > In my perl script I have a line like this.
> > system( "'/usr/bin/lp -dhp4si /tmp/plotreq.txt' > /tmp/plotid.txt");
You've got these quotes wrong: you're executing a command (via Bourne
shell) called '/usr/bin/lp -dhp4si /tmp/plotreq.txt', which I expect
doesn't exist on your system. :)
> > I have tried this also and did not work.
> > @lplist = ("/usr/bin/lp", "-dhp4si /tmp/plotreq.txt");
> > system(@lplist);
Here, you're passing an argument to 'lp'; the filename it's looking
for is '-dhp4si /tmp/plotreq.txt'. Again, I doubt that exists on
your system. :)
> >
> > hp4si is the destination printer.
> > /tmp/plotreq.txt is small text file to be sent to the printer.
> > /tmp/plotid.txt is the output of lp command ( just one line to indicate job
> > id )to be saved.
> > If I run the command /usr/bin/lp -dhp4si /tmp/plotreq.txt > /tmp/plotid.txt
> > it is fine as a command line.
Here, your shell is breaking out the commands/arguments on whitespace,
which is what you were hoping for.
You'd want something like (modulo shell metacharacters, etc.):
system qw('/usr/bin/lp -dhp4si /tmp/plotreq.txt > /tmp/plotid.txt');
But this doesn't get you STDERR, if there was any sortr of problem.
So, I do this:
use System2;
my @args = qw('/usr/bin/lp -dhp4si /tmp/plotreq.txt');
my ($out, $err) = system2(@args);
You could now check $? for whether or not your invocation to lp failed:
my ($exit_value, $signal_num, $dumped_core) = &System2::exit_status($?);
warn "lp choked!: $err" if $exit_value;
And, depending on the success/failure you have all of the output
of lp in STDOUT or STDERR, as appropriate.
> > This is the context.
> > sub search {
> > some code
> > ....
> > open(REQFILE, ">/tmp/plotreq.txt") || die "sorry, could not open
> > /tmp/plotreq.txt";
You should use $! to print _why_ this failed.
> > some more code to generate content for plotreq.txt
> > ......
> > close(REQFILE);
> > .... This is where I tried the above to send the file to the printer.
> > }
Note that you aren't making use of unique filenames. This is a
CGI program; you could hypothetically have multiple instances of
this code running at once.
Hope this helps...
> > Can somebody guide me to do it right ?.
> > Thanks
> > Raj kairam
> --
> J. J. Horner
> "H*","6a686f726e657240326a6e6574776f726b732e636f6d"
> ***************************************************
> "H*","6a6a686f726e65724062656c6c736f7574682e6e6574"
>
> Freedom is an all-or-nothing proposition: either we
> are completely free, or we are subjects of a
> tyrannical system. If we lose one freedom in a
> thousand, we become completely subjugated.
--
Brian 'you Bastard' Reichert <re...@numachi.com>
37 Crystal Ave. #303 Daytime number: (603) 434-6842
Derry NH 03038-1713 USA Intel architecture: the left-hand path
Re: Running a shell command inside a cgi/perl script
Posted by "J. J. Horner" <jh...@2jnetworks.com>.
* Kairam, Raj (KairamR@coned.com) [011130 15:10]:
> In my perl script I have a line like this.
> system( "'/usr/bin/lp -dhp4si /tmp/plotreq.txt' > /tmp/plotid.txt");
>
> hp4si is the destination printer.
> /tmp/plotreq.txt is small text file to be sent to the printer.
> /tmp/plotid.txt is the output of lp command ( just one line to indicate job
> id )to be saved.
>
> If I run the command /usr/bin/lp -dhp4si /tmp/plotreq.txt > /tmp/plotid.txt
> it is fine as a command line.
>
> The same in the perl script as above, doesn't send the file to printer nor
> does it create the /tmp/plotid.txt file.
>
> I have tried this also and did not work.
> @lplist = ("/usr/bin/lp", "-dhp4si /tmp/plotreq.txt");
> system(@lplist);
>
> This is the context.
> sub search {
> some code
> ....
> open(REQFILE, ">/tmp/plotreq.txt") || die "sorry, could not open
> /tmp/plotreq.txt";
> some more code to generate content for plotreq.txt
> ......
> close(REQFILE);
> .... This is where I tried the above to send the file to the printer.
> }
>
> This sub is part of a cgi script that creates output for the browser. The
> browser output is OK.
> The /tmp/plotreq.txt is generated but not being sent to the printer.
>
> Can somebody guide me to do it right ?.
> Thanks
> Raj kairam
>
Have you tried to 'su -' to nobody (or whomever your server is running as)
and run the command that way? Have you tried to run the script from the command-line
and see what that yields?
On a side note, you may want to ensure that you are using Taint for all of your CGI
scripts when they interact with the OS.
Thanks,
JJ
--
J. J. Horner
"H*","6a686f726e657240326a6e6574776f726b732e636f6d"
***************************************************
"H*","6a6a686f726e65724062656c6c736f7574682e6e6574"
Freedom is an all-or-nothing proposition: either we
are completely free, or we are subjects of a
tyrannical system. If we lose one freedom in a
thousand, we become completely subjugated.
Re: Running a shell command inside a cgi/perl script
Posted by Medi Montaseri <me...@CyberShell.com>.
Use the CHILD_ERROR $? to see what happend, for example
system(" some command ");
if ( $? )
{
....then figure out what happend..
}
Note that you'll have to see what your command returns and parse that.
See perlvar(1), there is some shifting around to be done as well.
But I am not sure how this is related to ModPerl?
On Fri, 30 Nov 2001, Kairam, Raj wrote:
> In my perl script I have a line like this.
> system( "'/usr/bin/lp -dhp4si /tmp/plotreq.txt' > /tmp/plotid.txt");
>
> hp4si is the destination printer.
> /tmp/plotreq.txt is small text file to be sent to the printer.
> /tmp/plotid.txt is the output of lp command ( just one line to indicate job
> id )to be saved.
>
> If I run the command /usr/bin/lp -dhp4si /tmp/plotreq.txt > /tmp/plotid.txt
> it is fine as a command line.
>
> The same in the perl script as above, doesn't send the file to printer nor
> does it create the /tmp/plotid.txt file.
>
> I have tried this also and did not work.
> @lplist = ("/usr/bin/lp", "-dhp4si /tmp/plotreq.txt");
> system(@lplist);
>
> This is the context.
> sub search {
> some code
> ....
> open(REQFILE, ">/tmp/plotreq.txt") || die "sorry, could not open
> /tmp/plotreq.txt";
> some more code to generate content for plotreq.txt
> ......
> close(REQFILE);
> .... This is where I tried the above to send the file to the printer.
> }
>
> This sub is part of a cgi script that creates output for the browser. The
> browser output is OK.
> The /tmp/plotreq.txt is generated but not being sent to the printer.
>
> Can somebody guide me to do it right ?.
> Thanks
> Raj kairam
>
>
--
-------------------------------------------------------------------------
Medi Montaseri medi@CyberShell.com
Unix Distributed Systems Engineer HTTP://www.CyberShell.com
CyberShell Engineering
-------------------------------------------------------------------------