Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

how to get short 'ls' output in a array using remote shell
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

Report this thread to moderator Post Follow-up to this message
Old Post
Wim Alsemgeest
09-11-04 01:55 PM


Re: how to get short 'ls' output in a array using remote shell
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;

Report this thread to moderator Post Follow-up to this message
Old Post
^x
09-11-04 08:56 PM


Re: how to get short 'ls' output in a array using remote shell
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

Report this thread to moderator Post Follow-up to this message
Old Post
Wolfgang Hommel
09-11-04 08:56 PM


Re: how to get short 'ls' output in a array using remote shell
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 "l
s
-l"` );
foreach my $file (@files)
{
print "$file ";
}



John
--
use Perl;
program
fulfillment

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
09-12-04 01:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PerlTk archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:11 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.