Re: Executing a unix command using file-handle



What you need is maybe this:

use strict;
use Data::Dumper;

open(WHO, "who|")or die "can't open who: $!";
my @arr = <WHO>;
close(WHO);

print Dumper \@arr;

for (@arr) {
print STDOUT (split)[2],"\n";
}


open (LS, "ls -l|") or die "can't open ls -l: $!";
@arr = <LS>;
close (LS);

shift @arr;
print Dumper \@arr;

print for @arr;


__END__

Good luck!



On Sun, Feb 24, 2008 at 4:15 PM, Anirban Adhikary
<anirban.adhikary@xxxxxxxxx> wrote:
Dear List
I am facing the following problem.While running the below mentioned code

open(WHO, "who|")or die "can't open who: $!";
@arr = <WHO>;
close(WHO);
print $arr[2]."\n";


I am getting this output --- root tty3 2008-02-24 12:58 . But
when i am trying to extract the 3 rd filed by using
$a = $arr[2]
@arr2 = split(/ /, $a);
print $arr2[2]."\n";

I am not getting any output . Please guide me

Another case when running the same code for ls -l/

open (LS, "ls -l|") or die "can't open ls -l: $!";
@arr = <LS>;
close (LS);
foreach(@arr)
{
@arr1 = split(/ /,$_);
}
foreach $perm (@arr1)
{print "$perm\n"; }

I am getting the last entry from ls -l listing. How can I get the entire
listing of ls -l command's output in an array??

Thanks & Regards In advance
Anirban Adhikary.

.