Perl: combine Backtick & system() I/O operation? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Perl: combine Backtick & system() I/O operation?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-30-2010
jeffw_00 jeffw_00 is offline
Registered User
 
Join Date: May 2010
Last Activity: 19 September 2010, 12:31 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Perl: Tee system-command to SYSOUT and @array? (not > file)

Hi - Within perl I want to execute a system command. I want to re-direct all the output from the command to a file (@result = `$cmd`, but I ALSO want the results to be displayed on the screen (system("$cmd");

The reason is this - if the command completes, I want to process the output. If the command -hangs- (trying to debug a system problem), I want the output on the screen so I can see how far the command got.

Any easy way to do this in perl?

Thanks!
/j

Last edited by jeffw_00; 05-30-2010 at 04:42 PM..
Sponsored Links
    #2  
Old 05-30-2010
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 17 April 2014, 10:58 PM EDT
Posts: 1,837
Thanks: 7
Thanked 265 Times in 240 Posts
Quote:
Originally Posted by jeffw_00 View Post
...
I want to re-direct all the output from the command to a file (@result = `$cmd`), but I ALSO want the results to be displayed on the screen (system("$cmd");
...
Any easy way to do this in perl?
...
You may want to check out the "Tee" module - Perl's implementation of the "tee" command.


Code:
$ 
$ # let's assume you want to run the command "seq 1 10" from within Perl
$ perl -le 'use Tee; tee("seq 1 10", "testfile.out")'
1
2
3
4
5
6
7
8
9
10
$ 
$ cat testfile.out
1
2
3
4
5
6
7
8
9
10
$

tyler_durden
Sponsored Links
    #3  
Old 05-30-2010
jeffw_00 jeffw_00 is offline
Registered User
 
Join Date: May 2010
Last Activity: 19 September 2010, 12:31 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
well, first, apparently ActivePerl doesn't support Tee. But if it did, are you saying this would work?


Code:
#!/usr/bin/perl -w

use Tee;

tee("dir",@results); # puts output of "dir" results to screen, and into @results

foreach $line (@results) {  # now print the results again just to show they were captured in the array
    print $line;
}

thanks
/j

Last edited by Scott; 05-30-2010 at 06:56 PM.. Reason: Code tags, please...
    #4  
Old 05-30-2010
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 17 April 2014, 10:58 PM EDT
Posts: 1,837
Thanks: 7
Thanked 265 Times in 240 Posts
No, that second argument is either a filename or an array of filenames to which the output of command ("dir") would be redirected.


Code:
$ # print the output of "dir" on the screen
$ # and also redirect the output of "dir" to the file "file1.out"
$ perl -le 'use Tee; tee("dir", "file1.out")'
testfile  testfile.out
$ 
$ # check the content of the file "file1.out" now
$ cat file1.out
testfile  testfile.out
$ 
$ # the same as what was printed on the screen

Same thing with an array of filenames:


Code:
$ 
$ # print the output of "dir" on the screen
$ # and also redirect the output of "dir" to the files in the array
$ # "@files" i.e. file2.out, file3.out and file4.out
$ perl -le 'use Tee; @files = qw (file2.out file3.out file4.out); tee("dir", @files)'
file1.out  testfile  testfile.out
$ 
$ # now check the contents of file2.out, file3.out and file4.out
$ cat file2.out
file1.out  testfile  testfile.out
$ 
$ cat file3.out
file1.out  testfile  testfile.out
$ 
$ cat file4.out
file1.out  testfile  testfile.out
$ 
$

tyler_durden
Sponsored Links
    #5  
Old 05-30-2010
jeffw_00 jeffw_00 is offline
Registered User
 
Join Date: May 2010
Last Activity: 19 September 2010, 12:31 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Ummm - well thanks then. Although a useful answer, it doesn't match my question .

Is there any way to direct the output of a system cmd to an array and the screen simultaneously?
Sponsored Links
    #6  
Old 05-30-2010
pseudocoder's Avatar
pseudocoder pseudocoder is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 17 February 2012, 6:42 AM EST
Location: /home/sea
Posts: 602
Thanks: 40
Thanked 83 Times in 81 Posts
Quote:
Originally Posted by jeffw_00 View Post
well, first, apparently ActivePerl doesn't support Tee.
Maybe not out-of-the-box, but it certainly can be installed from CPAN, check the ppm command
Quote:
Originally Posted by jeffw_00 View Post
But if it did, are you saying this would work?
Nope. Check the Tee CPAN page for syntax: Tee @ CPAN

---------- Post updated at 21:48 ---------- Previous update was at 21:21 ----------

Try this:

Code:
#!/usr/bin/perl

use strict;
use warnings;

my @arr;

print "Output from screen:\n";

open(FROM, "ls |") or die "Could not open pipe: $!\n";

while (<FROM>) {
chomp;
print "$_\n";
push @arr, $_;
}

close(FROM);

print "Output from array:\n";

 foreach (@arr) {
 chomp;
 print "$_\n";
 }

Sponsored Links
    #7  
Old 05-30-2010
jeffw_00 jeffw_00 is offline
Registered User
 
Join Date: May 2010
Last Activity: 19 September 2010, 12:31 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
pseudocoder - that looks like it would work, but I wonder when the command is actually executed. If it's executed during the OPEN and then hangs, I don't think it will ever get to the first print statement?

thanks though

I will look into downloading tee - adding packages is new to me though
/j
===================
Update - you know - I think what I'm looking for doesn't exist
tee($command,@array) where the output of the command goes to STDOUT and into the @array. all the Tee that I can find at CPAN writes the output to -files-. I suppose I could create a temp file and then read it back in, but that's sort of inelegant?

am I missing something here?

thanks!
/j
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl combine if jimmy_y Shell Programming and Scripting 2 03-29-2010 01:03 PM
CVS file operation in Perl gentleDean Shell Programming and Scripting 1 11-04-2009 06:45 AM
combine two perl lines into a single perl command jimmy_y Shell Programming and Scripting 4 06-14-2009 11:56 PM
where can i get the unix operation system? tcb3210 Ubuntu 2 09-14-2006 12:33 PM
Multi operation system in one pc. HOUSCOUS UNIX Desktop for Dummies Questions & Answers 1 07-04-2002 10:51 PM



All times are GMT -4. The time now is 08:11 AM.