This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.
To: "J. J. Horner" <jhorner@2jnetworks.com> From: Brian Reichert <reichert@numachi.com> Subject: Re: Running a shell command inside a cgi/perl script Date: Fri, 30 Nov 2001 16:23:41 -0500 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. === To: "Kairam, Raj" <KairamR@coned.com>, <modperl@apache.org> From: "Rodney Broom" <mod_perl@rbroom.com> Subject: Re: Running a shell command inside a cgi/perl script Date: Fri, 30 Nov 2001 13:56:05 -0700 From: Kairam, Raj <KairamR@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(). ===