[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-modperl
Subject:    Re: Running a shell command inside a cgi/perl script
From:       Brian Reichert <reichert () numachi ! com>
Date:       2001-11-30 21:23:41
[Download RAW message or body]

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		<reichert@numachi.com>
37 Crystal Ave. #303			Daytime number: (603) 434-6842
Derry NH 03038-1713 USA			Intel architecture: the left-hand path

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic