Gossamer Forum
Home : General : Perl Programming :

Need help with a simple function

Quote Reply
Need help with a simple function
I am trying to keep a backup script from failing. I need to check for a certain file type ie. ".being-deleted" in a nested directory structure. If any file of type "being-deleted" is found I exit the script.



I am new at Perl programming and have no clue where to start.



Thanks for your help.
Quote Reply
Re: [Rocky99] Need help with a simple function In reply to
Code:
if (-e "/path/to/some.file") {
print "Found the file. Can't continue!";
exit 1;
}

Here are some other possibilities....

-r File is readable by effective uid/gid.
-w File is writable by effective uid/gid.
-x File is executable by effective uid/gid.
-o File is owned by effective uid.
-R File is readable by real uid/gid.
-W File is writable by real uid/gid.
-X File is executable by real uid/gid.
-O File is owned by real uid.
-e File exists.
-z File has zero size.
-s File has non-zero size (returns size).
-f File is a plain file.
-d File is a directory.
-l File is a symbolic link.
-p File is a named pipe (FIFO).
-S File is a socket.
-b File is a block special file.
-c File is a character special file.
-u File has setuid bit set.
-g File has setgid bit set.
-k File has sticky bit set.
-t Filehandle is opened to a tty.
-T File is a text file.
-B File is a binary file (opposite of -T).
-M Age of file in days when script started.
-A Same for access time.
-C Same for inode change time.

If you wish to use more than one option on the same file you should use an underscore after the first test, ie:

if (-e "/path/to/some.file" or -d _ or -z _) {
Quote Reply
Re: [Paul] Need help with a simple function In reply to
Or use the File::Stat module which is more portable for other operating systems, such as Win32. :-)

- wil
Quote Reply
Re: [Wil] Need help with a simple function In reply to
Umm those options work on Win32 Tongue ...at least the ones I've bothered to try.
Quote Reply
Re: [Paul] Need help with a simple function In reply to
Thanks for your help,

How would I do this if the file is unknown name.being-deleted and I want to search through a directory structure? The names change but the type of the file is the same.

I really appreciate your help.
Quote Reply
Re: [Paul] Need help with a simple function In reply to
Paul, nice quick reference.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Rocky99] Need help with a simple function In reply to
Now for this I would use a module :)

File::Find

http://www.perldoc.com/...e/Find.html#SYNOPSIS
Quote Reply
Re: [Paul] Need help with a simple function In reply to
I knew I'd turn you in the end ;-)

- wil
Quote Reply
Re: [Rocky99] Need help with a simple function In reply to
Here's a quick example as File::Find can be a little tricky to understand just by reading the perldoc alone:

Code:
#!/usr/bin/perl
#============================

use strict;
use File::Find;

main();

#============================

sub main {
#---------------------------------------------
# Start finding stuff.

my @found = ();

find( sub { /\.cgi$/ && push @found, $File::Find::name }, '/');

print "Content-type: text/html\n\n";
print join "<br>\n", @found;
}

Which will find cgi scripts.

Last edited by:

Paul: Jun 17, 2002, 11:32 AM
Quote Reply
Re: [Paul] Need help with a simple function In reply to
Perfect. I can now scan for a specific type file but I am trying to add a variable the would point to a specific directory path. This way if the directory structure changes all I have to do is change the variable. Obviuos for all but me on where I should place the variable.Unsure

example code:

#!/usr/bin/perl
#============================

use strict;
use File::Find;
my $DIRS
main();

#============================

sub main {
#---------------------------------------------
# Start finding stuff.

my @found = ();

find( sub { $DIR/\.cgi$/ && push @found, $File::Find::name }, '/');

print "Content-type: text/html\n\n";
print join "<br>\n", @found;
}

The stuff in red is my weak attempt to specify a directory path. Thanks for all your help.
Quote Reply
Re: [Rocky99] Need help with a simple function In reply to
Under:

my @found = ();

...add something like:

my $DIR = '/path/to/you/dir'; # No trailing slash.

Then change:

find( sub { /\.cgi$/ && push @found, $File::Find::name }, '/');

to:

find( sub { /\.cgi$/ && push @found, $File::Find::name }, $DIR);

>>$DIR/\.cgi$/<<

/\.cgi$/ isn't a directory path, it is a regex :)

Last edited by:

Paul: Jun 18, 2002, 10:03 AM
Quote Reply
Re: [Paul] Need help with a simple function In reply to
Thanks for all your help. After a few minutes of tinkering with a test.pl script it works perfect.

thanks againSly