For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > September 2004 > how to get short 'ls' output in a array using remote shell









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to get short 'ls' output in a array using remote shell
Wim Alsemgeest

2004-09-11, 8:55 am

Dear perl programmers,

As a beginning perl programmer I like to do as follows.
Create a array @myfiles and fill it with files in a directory. The
sub-directory's should be skipped.

In the test directory there are fourfiles. One is a directory.
A normal ls -l displays:

total 8
-rwxr-xr-x 1 Prive1 None 6957 Aug 30 20:13 grid.pl
drwxr-xr-x 2 Prive1 None 0 Sep 8 20:20 test
-rwxr-xr-x 1 Prive1 None 125 Sep 8 20:21 test.pl
-rw-r--r-- 1 Prive1 None 0 Sep 11 2004 testje.txt



As I perform my script on a local machine, it is working fine:

#!/usr/bin/perl -w
my @files = `ls -l | grep -v \"^d\" | awk \'{print \$9\}\'`;
foreach $file (@files)
{
print "$file ";
}

The output is:
grid.pl
test.pl
testje.txt

the subdir test is not displayd



If I perform the perlscript in a remote shel the filter for the
directorys still works, but the awk dousn't work anymore.

#!/usr/bin/perl -w
my @files = `remsh foo \"ls -l | grep -v \"^d\" | awk \'{print \$9\}\'\"`;
foreach $file (@files)
{
print "$file ";
}

The output is now:
-rwxr-xr-x 1 Prive1 None 6957 Aug 30 20:13 grid.pl
-rwxr-xr-x 1 Prive1 None 125 Sep 8 20:21 test.pl
-rw-r--r-- 1 Prive1 None 0 Sep 11 2004 testje.txt

The output should be:
grid.pl
test.pl
testje.txt

How can I get a array from all the regular files in a directory on a
remote server.
I like to use the backtag methode `command` andnot the
use::Net::Rsh
use::Net::Ssh

Greetings,
Wim Alsemgeest
walsemge@kabelfoon.nl
^x

2004-09-11, 3:56 pm

Wim Alsemgeest wrote:
> Dear perl programmers,
>
> As a beginning perl programmer I like to do as follows.
> Create a array @myfiles and fill it with files in a directory. The
> sub-directory's should be skipped.


try
opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@files = readdir(DIR);
closedir DIR;
Wolfgang Hommel

2004-09-11, 3:56 pm

Hello Wim,

> If I perform the perlscript in a remote shel the filter for the
> directorys still works, but the awk dousn't work anymore.
>
> #!/usr/bin/perl -w
> my @files = `remsh foo \"ls -l | grep -v \"^d\" | awk '{print \$9\}'\"`;
> foreach $file (@files)
> {
> print "$file ";
> }


As you're using the backticks, it's more a portable shell programming
than a Perl problem. It definitely ain't a Tk issue :-)

Anyway, consider

a) using local awk vs. using remote awk

b) using "find ... -type f" instead of "grep -v ^d", because not
everything that's not a directory is a file (e.g., it could be a link)

c) placing a simple shell or perl script on the remote host which
outputs the data you need, and then calling this script through ssh
instead of the longish command sequence.


Regards,
Wolfgang
John W. Krahn

2004-09-11, 8:56 pm

Wim Alsemgeest wrote:
>
> As a beginning perl programmer I like to do as follows.
> Create a array @myfiles and fill it with files in a directory. The
> sub-directory's should be skipped.
>
> In the test directory there are fourfiles. One is a directory.
> A normal ls -l displays:
>
> total 8
> -rwxr-xr-x 1 Prive1 None 6957 Aug 30 20:13 grid.pl
> drwxr-xr-x 2 Prive1 None 0 Sep 8 20:20 test
> -rwxr-xr-x 1 Prive1 None 125 Sep 8 20:21 test.pl
> -rw-r--r-- 1 Prive1 None 0 Sep 11 2004 testje.txt
>
> As I perform my script on a local machine, it is working fine:
>
> #!/usr/bin/perl -w
> my @files = `ls -l | grep -v \"^d\" | awk '{print \$9\}'`;
> foreach $file (@files)
> {
> print "$file ";
> }
>
> The output is:
> grid.pl
> test.pl
> testje.txt
>
> the subdir test is not displayd
>
> If I perform the perlscript in a remote shel the filter for the
> directorys still works, but the awk dousn't work anymore.
>
> #!/usr/bin/perl -w
> my @files = `remsh foo \"ls -l | grep -v \"^d\" | awk '{print \$9\}'\"`;
> foreach $file (@files)
> {
> print "$file ";
> }
>
> The output is now:
> -rwxr-xr-x 1 Prive1 None 6957 Aug 30 20:13 grid.pl
> -rwxr-xr-x 1 Prive1 None 125 Sep 8 20:21 test.pl
> -rw-r--r-- 1 Prive1 None 0 Sep 11 2004 testje.txt
>
> The output should be:
> grid.pl
> test.pl
> testje.txt
>
> How can I get a array from all the regular files in a directory on a
> remote server.
> I like to use the backtag methode `command` andnot the
> use::Net::Rsh
> use::Net::Ssh


This should be close to what you want:

#!/usr/bin/perl -w
chomp( my @files = map +( split ' ', $_, 9 )[ 8 ], grep !/^d/, `remsh foo "ls
-l"` );
foreach my $file (@files)
{
print "$file ";
}



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com