|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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 screenSame 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
|
|||
|
|||
|
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
|
||||
|
||||
|
Maybe not out-of-the-box, but it certainly can be installed from CPAN, check the ppm command 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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|