CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
-x file tests failing

 



s660117
User

Jul 6, 2010, 10:34 AM

Post #1 of 5 (857 views)
-x file tests failing Can't Post

Hi,
I have a little Perl script that I wrote as an exercise. It is a variation on grep that searches subfolders as well as the current directory. This script works fine on Windows, but I'm having a problem under Suse with the statement that checks to see if the current directory is a symlist or is equal to "dev". The script follows -

#! /usr/bin/perl -w
if (@ARGV) {($dir,$str) = @ARGV;}
-d $dir || die "Terminating. $!";
print "Now scanning $dir for \"$str\"\n";
$currdir = $dir;
chdir ($currdir) || die "Failed to chdir to $currdir. $!";

while ($dirent = <*>) {
if ($dirent =~ /^$str/) {print "$dirent found in $currdir\n";}
if (-d "$dirent" && !(-l "$dirent") && $dirent ne "dev") {
srchdir();
# chdir '..' || die "Chdir .. failed $!";;
chomp($currdir = `pwd`);
}
}

sub srchdir {
$currdir = "$currdir"."\/"."$dirent";
chdir ("$dirent") || die "Chdir .. failed. $!";
while ($dirent = <*>) {
if ($dirent =~ /^$str/) {print "$dirent found in $currdir\n";}
if (-d "$dirent" && !(-l "$dirent") && $dirent ne "dev") {
srchdir();
# chdir '..' || die "Chdir .. failed $!";
chomp($currdir = `pwd`);
}
}
}

If I run the script against /usr, for example, looking for a file that exists in /bin, it finds it and prints a message to that effect. It fails, however, to look in any subsequent directories such as /include because the statement

if (-d "$dirent" && !(-l "$dirent") && $dirent ne "dev") {

then resolve to false. If I put in some diagnostic checks -

$directory = "$currdir"."\/"."$dirent";
if (-e "$directory") {print " $directory exists\n";}
else {print " $directory does not exist\n";}
if (-d "$directory") {print " $directory is a directory\n";}
else {print " $directory is not a directory\n";}
if (!(-l "$directory")) {print " $directory is not a symlist\n";}
if ($dirent ne "dev") {print " $directory is not dev\n";}

I get the following using /include as an example -

/usr/bin/include does not exist
/usr/bin/incdlue not a directory
/usr/bin/include is not a symlist
/usr/bin/include is not dev.

And for /tmp, which is a symlist, I get -

/usr/bin/tmp does not exist
/usr/bin/tmp is not a directory
/usr/bin/tmp is not a symlist
/usr/bin/tmp is not dev.

I don't understand why the -x file tests are failing and would appreciate any help in this matter.
Thanks,
s660117


s660117
User

Jul 6, 2010, 3:27 PM

Post #2 of 5 (836 views)
Re: [s660117] -x file tests failing [In reply to] Can't Post

One odd thing I just noticed -
If I insert the diagnostic code immediately before the statement

if (-d "$dirent" && !(-l "$dirent") && $dirent ne "dev") {

AND comment out the first call to the srchdir subroutine (which calls itself recursively), the -x file tests work as expected.


rovf
Veteran

Jul 8, 2010, 3:19 AM

Post #3 of 5 (818 views)
Re: [s660117] -x file tests failing [In reply to] Can't Post

Call me blind, but I don't see that you are testing something with "-x".

Your -d test fails, which means that /usr/bin/include is not a directory (which is what I would expect, because unless you have explictily created it, I would not expect an "include" directory on /usr/bin).

If you do a

ls -la /usr/bin/include

from the shell, what do you get?


FishMonger
Veteran / Moderator

Jul 8, 2010, 5:39 AM

Post #4 of 5 (815 views)
Re: [s660117] -x file tests failing [In reply to] Can't Post

If you use the File::Find module, you'll be able to simplify your script, and remove about 50% of that code.

http://search.cpan.org/~jesse/perl-5.12.1/lib/File/Find.pm


(This post was edited by FishMonger on Jul 8, 2010, 5:39 AM)


s660117
User

Jul 8, 2010, 1:28 PM

Post #5 of 5 (799 views)
Re: [s660117] -x file tests failing [In reply to] Can't Post

Thanks for the replies.
FishMonger... I didn't use File::Find module because the aim of the exercise was to write a "find" command using recursion.
rovf... Right you are: there's no such directory as /usr/bin/include and that was the problem with the file tests. The script was failing to bump up one entry in the directory tree after processing the current directory.
chdir '..' || die "Chdir .. failed $!";
fixed that. I also found that I needed to substitute
foreach $dirent (<*>) {
for the while command.
The script, such as it is, that works is ---

#! /usr/bin/perl -w
use warnings;

if (@ARGV) {($dir,$str) = @ARGV;}
-d $dir || die "Terminating. $!";
print "Now scanning $dir for \"$str\"\n";
$currdir = $dir;
chdir ("$currdir") || die "Failed to chdir to $currdir. $!";

foreach $dirent (<*>) {
if ($dirent =~ /^$str/) {print "B $dirent found in $currdir\n";}
if (-d "$dirent" && !(-l "$dirent") && $dirent ne "dev") {

srchdir();
chdir '..' || die "Chdir .. failed $!";
chomp($currdir = `pwd`);
}
}

sub srchdir {
$currdir = "$currdir"."\/"."$dirent";
chdir ("$currdir") || die "Failed to chdir to $dirent. $!";

foreach $dirent (<*>) {
if ($dirent =~ /^$str/) {print "I $dirent found in $currdir\n";}
if (-d "$dirent" && !(-l "$dirent") && $dirent ne "dev") {

srchdir();
chdir '..' || die "Chdir .. failed $!";
chomp($currdir = `pwd`);
}
}
}

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives