tags:

views:

79

answers:

2

I want to uninstall a package in Solaris, say NewPackage. For that I am using the command:

pkgrm NewPackage

It will display all the steps in the STDOUT and at the end it will give the message that the package is uninstalled successfully.

If i want to uninstall the same package in a Perl program how do I redirect the STDOUT messages to a file so that at the end of the execution I can go to the file and verify the file. Presently i tried the following commands without success:

open (FD, "/usr/tmp/result.txt");
$input1 = <FD>;
$input2 = <FD>;
system("pkgrm NewPackage" < $input1);

But don't know how to pass the second input.

Please advice how to proceed.

+2  A: 

In general:

  • use the strict and warnings pragmas
  • use 3 argument open with a lexical handle
  • test to see if your open call succeeds before continuing your script
  • if you are numbering your variable names ($input1, $input2) use an array instead (@inputs)
  • if you want to use a variable name as a variable, use a hash instead
  • when in doubt, RTFM. Since TFM is big and hard to find things in at first, you might want to read How To RTFM
  • The FAQs are pretty handy too. There's a whole section on system interaction, and another on file manipulation.
  • when you need a function to do some random task, look at the functions by category section in perlfunc.

No matter what, you will need to define what you are trying to do a little more specifically.

Do you want to run your command and dump its STDOUT to a file:

`pkgrm $package > $outfile`;

Or maybe append the results to an existing log:

`pkgrm $package >> $outfile`;

Or do you want to catch the results and see what happened in your program:

open( my $result_fh, '>', $outfile ) or die "can't open logfile - $!\n";
my $output = `pkgrm $package`;
if( $oupt =~ /happiness/) {
   print $result_fh "hooray!\n", $output;
}
else {

   print $result_fh "uh oh\n", $output;
}

Or do you need to monitor standard error?

Or should STDERR and STDOUT be combined into one stream?

For these cases and more see perlfaq8.

daotoad
A: 

Your question has the same answer as How do I get the output of an external command in Perl?.

brian d foy