北京网站设计套餐,简历表格 个人简历手机版,wordpress 股票主题,网址搭建wordpress前言
写了一个脚本可以同时检查多个仿真log文件#xff0c;并生成html表格。按照文件修改时间从新到旧排序。但是一直无法使用stat函数获取修改时间。
结论#xff1a;find函数会改变程序执行的当前目录#xff0c;find(\process_files, $dir);函数是在$dir目录下运行…前言
写了一个脚本可以同时检查多个仿真log文件并生成html表格。按照文件修改时间从新到旧排序。但是一直无法使用stat函数获取修改时间。
结论find函数会改变程序执行的当前目录find(\process_files, $dir);函数是在$dir目录下运行的。
正文
测试环境的目录结构如下
.
├── check_logs.pl
└── logs├── 1.txt├── 2.txt└── 3.txt1 directory, 4 files一、使用内置函数stat
perl提供一个内置函数stat()获取文件相关信息函数返回一个数组。 官方文档介绍stat - Perldoc 浏览器 my($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) stat($file_name);介绍几个比较重要的含义
$dev和$ino 文件所在设备的编号和文件的inode编号。 $mode 文件的权限位集合还包含其他信息位。低9位是linux的权限位。 $nlink 文件或目录的硬连接数。 $uid和$gid 以数值形式表示文件拥有者的用户ID和组ID $size 以字节为单位的文件大小 $atime$mtime和$ctime 三种时间戳一个32位的整数表示从1970年开始的秒数。访问时间atime访问时间记录了文件最后一次被读取的时间。每当文件被读取时其访问时间戳就会被更新。这对于某些应用程序来说是有用的例如日志审计或跟踪文件的访问频率。修改时间mtime修改时间记录了文件内容最后一次被修改的时间。当文件的内容数据被修改时其修改时间戳就会被更新。这对于确定文件的最后修改时间非常有用。更改时间ctime更改时间记录了文件元数据最后一次被更改的时间。元数据是与文件相关的非数据信息例如文件的权限、所有者或文件类型等。当这些元数据属性发生变化时其更改时间戳就会被更新。
先看下使用内置函数获取修改时间的代码
#! /bin/perl -w
use strict;
use warnings;
use File::Find;
use File::Basename;my $time (stat(./logs/1.txt))[10];
print $time\n;运行结果如下
[fengbhVM-16-14-centos perl_stat]$ ./check_logs.pl
1703579691二、使用File::stat
File::stat会覆盖内置的系统函数它以类的方式提供类似内置函数stat的功能。 官方文档File::stat - by-name interface to Perl’s built-in stat() functions - Perldoc Browser 使用类的方式获取修改时间的代码如下
#! /bin/perl -w
use strict;
use warnings;
use File::Find;
use File::stat;my $time stat(./logs/1.txt)-mtime;
print $time\n;运行结果如下
[fengbhVM-16-14-centos perl_stat]$ ./check_logs.pl
1703579691三、在File::Find中使用
这里使用内置函数的方式实现。
#! /bin/perl -w
use strict;
use warnings;
use File::Find;my $dir ./logs;
find(\process_files, $dir);sub process_files{return if !-f $_;#debugprint \$_ $_\n;print \$File::Find::name $File::Find::name\n\n;#get mtimemy $mtime (stat($File::Find::name))[10];die Cant stat file;$!\n if !defined($mtime);# debugprint mtime $mtime\n;
}运行结果
[fengbhVM-16-14-centos perl_stat]$ ./check_logs.pl
$_ 3.txt
$File::Find::name ./logs/3.txtCant stat file;No such file or directory运行发现报错找不到文件。但是传给stat函数的文件路径名是正确的。
这是因为find函数会改变程序执行的当前目录或者可以理解为process_files函数是在$dir目录下运行的。
这就是报错的原因$File::Find::name是相对于初始执行目录的路径$_才是相对于$dir的路径。
将代码修改为
#! /bin/perl -w
use strict;
use warnings;
use File::Find;my $dir ./logs;
find(\process_files, $dir);sub process_files{return if !-f $_;#debugprint \$_ $_\n;print \$File::Find::name $File::Find::name\n\n;#get mtimemy $mtime (stat($_))[10];die Cant stat file;$!\n if !defined($mtime);# debugprint mtime $mtime\n;
}执行结果如下
[fengbhVM-16-14-centos perl_stat]$ ./check_logs.pl
$_ 3.txt
$File::Find::name ./logs/3.txtmtime 1703577429
$_ 1.txt
$File::Find::name ./logs/1.txtmtime 1703579691
$_ 2.txt
$File::Find::name ./logs/2.txtmtime 1703577426执行结果正确
参考文献 官方文档介绍stat - Perldoc 浏览器官方文档File::stat - by-name interface to Perl’s built-in stat() functions - Perldoc Browser《perl语言入门》