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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: combine Backtick & system() I/O operation?
# 1  
Old 05-30-2010
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`Smilie, 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 05:42 PM..
# 2  
Old 05-30-2010
Quote:
Originally Posted by jeffw_00
...
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
# 3  
Old 05-30-2010
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 07:56 PM.. Reason: Code tags, please...
# 4  
Old 05-30-2010
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
# 5  
Old 05-30-2010
Ummm - well thanks then. Although a useful answer, it doesn't match my question Smilie.

Is there any way to direct the output of a system cmd to an array and the screen simultaneously?
# 6  
Old 05-30-2010
Quote:
Originally Posted by jeffw_00
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
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";
 }

# 7  
Old 05-30-2010
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help me to perform count & group by operation in shell scripting?

Hi All, I want to display the distinct values in the file and for each distinct value how may occurance or there. Test data: test1.dat 20121105 20121105 20121105 20121105 20121106 20121106 20121106 20121105 I need to display the output like Output (2 Replies)
Discussion started by: bbc17484
2 Replies

2. AIX

Monitoring AIX with System Center Operation Manager 2007 R2

I can get my AIX servers to show up in SCOM but I'm struggling with running personal scripts and have the result of these scripts show up in SCOM. I'm currently using BigBrother and I've written some scripts for BB. I would like to convert these scripts to something that can be used in SCOM. ... (0 Replies)
Discussion started by: petervg
0 Replies

3. Shell Programming and Scripting

Enter third column & Perform Operation

I am trying to enter a third column in this file, but the third column should that I call "Math" perform a some math calculations based on the value found in column #2. Here is the input file: Here is the desired output: Output GERk0203078$ Levir Math Cotete_1... (5 Replies)
Discussion started by: Ernst
5 Replies

4. UNIX for Dummies Questions & Answers

system calls and atomic operation

Are system calls atomic operations? Is a system call can be interrupted? (2 Replies)
Discussion started by: vistastar
2 Replies

5. Linux

commands of linux operation system

Am actually am new to the linux operating system and knda wana knw more about it.Please could u help me with some commands so i can start with that first.Really need your help (5 Replies)
Discussion started by: GODBLESSME
5 Replies

6. Shell Programming and Scripting

Perl: Backtick Errors

When trying to use backticks for system commands, is there a way to read the error messages if a command doesn't execute properly? I have no problem getting the results if the command is properly executed. Ex. my @result = `dir`; foreach my $line (@result) { print "Result = $line";... (2 Replies)
Discussion started by: kooshi
2 Replies

7. Shell Programming and Scripting

combine two perl lines into a single perl command

Hi Everyone, i have a string 00:44:40 so: $tmp=~ s/://gi; $tmp=~s/({2})({2})({2})/$1*3600+$2*60+$3/e; the output is 2680. Any way to combine this two lines into a single line? Thanks (4 Replies)
Discussion started by: jimmy_y
4 Replies

8. UNIX for Dummies Questions & Answers

what does the operation '>&' mean in a shell script

hi, all, i saw a syntax like this usr/local/mpiexec -np 8 /home/XXXX/program_exe >& ./temp/out the output will be put into ./temp/out. I want to know the meaning of the operation symbol '>&'. Thanks (3 Replies)
Discussion started by: cy163
3 Replies

9. Ubuntu

where can i get the unix operation system?

I am a stranger here ,where can i get the operation for free.thank you! (2 Replies)
Discussion started by: tcb3210
2 Replies

10. UNIX Desktop Questions & Answers

Multi operation system in one pc.

Does anyone can write a thread to teach me how to install Windows 2000, RH Linux, and FreeBSD unix in one machine step by step? Or someone's already done with this topic? (1 Reply)
Discussion started by: HOUSCOUS
1 Replies
Login or Register to Ask a Question