Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageWim 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;
Post Follow-up to this messageHello 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
Post Follow-up to this messageWim 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 "l
s
-l"` );
foreach my $file (@files)
{
print "$file ";
}
John
--
use Perl;
program
fulfillment
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.