- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Perl's -M file Test operator
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-16-2005 02:35 PM
тАО05-16-2005 02:35 PM
I want to do something with files created in the last X days. (I have used 17 in this example to pick up files from this month.)
For the directory in question there is a bunch of matching files going back years.
Why when I execute this code is the "false" line aways printed???
____________
#! /usr/bin/perl
$aDir = '/tmp';
@result = `find $aDir -name \"*error.log\"`;
foreach $file (@result) {
if ((-M $file) > 16.0)
{ $tmp = `ls -l $file`; print "true: $tmp"; }
else
{ $tmp = `ls -l $file`; print "false: $tmp"; }
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-16-2005 06:27 PM
тАО05-16-2005 06:27 PM
Solutionlt09:/home/merijn/itrc 128 > perl -MFile::Find -le'find(sub{/\.log$/ and$res{$File::Find::name}=[stat]},"/tmp");for(keys%res){$x=-M$_<16.0?"True":"False";print scalar localtime$res{$_}[9]," $_ ($x)"}'
Tue Mar 22 14:16:04 2005 /tmp/kde-merijn/konqueror-crash-o4ueGb.log (False)
Sun May 8 12:05:28 2005 /tmp/p59.log (True)
Sun May 1 18:33:50 2005 /tmp/xine-plugin.log (True)
Sun May 1 18:33:42 2005 /tmp/Download/hs_err_pid25962.log (True)
Thu Dec 16 18:53:44 2004 /tmp/Download/gnokii-0.6.4/config.log (False)
Tue May 3 23:45:29 2005 /tmp/kde-merijn/konqueror-crash-9qeahb.log (True)
Thu May 12 23:24:43 2005 /tmp/kde-merijn/konqueror-crash-J39zLb.log (True)
Tue Mar 22 19:36:35 2005 /tmp/kde-merijn/konqueror-crash-6ugIvb.log (False)
Tue Apr 19 19:02:41 2005 /tmp/kde-merijn/konqueror-crash-AVMgqb.log (False)
Tue May 3 23:51:23 2005 /tmp/kde-merijn/konqueror-crash-YA25Xa.log (True)
Tue Apr 19 14:04:19 2005 /tmp/kde-merijn/konqueror-crash-USJMgc.log (False)
Fri Apr 22 20:41:55 2005 /tmp/kde-merijn/konqueror-crash-zP78wb.log (False)
Mon Oct 13 20:40:06 2003 /tmp/Download/avifile-0.7-0.7.38/config.log (False)
Tue May 3 20:13:38 2005 /tmp/kde-merijn/konqueror-crash-IILVNb.log (True)
lt09:/home/merijn/itrc 129 >
Or in a script:
--8<---
use strict;
use warnings;
use File::Find;
my %res;
find (sub {
m/\.log$/ and $res{$File::Find::name} = [ stat ];
},"/tmp");
foreach my $file (keys %res) {
my $x = -M $_ < 16.0 ? "True" :"False";
print scalar localtime $res{$_}[9], " $_ ($x)\n";
}'
-->8---
Sorry I switched true and false, but I guess you get the drift here
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-16-2005 06:35 PM
тАО05-16-2005 06:35 PM
Re: Perl's -M file Test operator
I will update just how well it works tomorrow before closing the thread and assigning points.
I am curious however to know what was wrong with my code? (apart from relying to much on commands designed for ues in a shell.) The relevant pages of may camel book are quite warn!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-16-2005 06:44 PM
тАО05-16-2005 06:44 PM
Re: Perl's -M file Test operator
If you take a real close look at the entries in @result, you'd see that the file names have trailing new lines :)
chomp (@result = grep /error\.log$/ => `find /tmp`);
would have done it your way
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-16-2005 06:59 PM
тАО05-16-2005 06:59 PM
Re: Perl's -M file Test operator
foreach $file (@result) {
chomp $file;
...
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-17-2005 02:30 PM
тАО05-17-2005 02:30 PM
Re: Perl's -M file Test operator
"warnings.pm" does not seem to be available on the system I am working on which seemed a little odd. I presume this is normally part of the standard distribution?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-17-2005 03:33 PM
тАО05-17-2005 03:33 PM
Re: Perl's -M file Test operator
It is a replacement for command line flag -w, and while -w flag is global, warnings.pm can be used to enable/disable warnings for a block of code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-17-2005 06:06 PM
тАО05-17-2005 06:06 PM
Re: Perl's -M file Test operator
warnings was first released with perl 5.006
5.006 undef
5.006001 undef
5.006002 undef
5.007003 1.00
5.008 1.00
5.008001 1.03
5.008002 1.03
5.008003 1.03
5.008004 1.03
5.008005 1.03
5.008006 1.03
5.009 1.03
5.009001 1.03
5.009002 1.04
The big advantage of 'use warnings' over -w, is that use warnings is lexically scoped. That means that if you use -w (which is good), it would also promote the warnings setting to all modules and scripts used from the calling process, whereas 'use warnings' only affects the current scope, and you can even put off subsets of warnings for (sub) blocks where you know things would warn.
# perldoc warnings
# perldoc perllexwarn
for a more elaborate explanation and examples that make sense. The latter also describes wich categories can be enabled or disabled.
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-17-2005 06:16 PM
тАО05-17-2005 06:16 PM
Re: Perl's -M file Test operator
I have learnt a lot from your input.
I promise not to use the external find from within Perl ;-)
(I am a tad embarrased about not picking the new lines as the culprit for why my original code was not working as I have been caught by this previously.)
The current platform is perl5.005_03 which explains why warnings.pm was not present.