工具小脚本
本文转自博客园刘凯毅的博客,原文链接:perl 工具小脚本,如需转载请自行联系原博主。
#
!/bin/perl
use File :: Find;
use Log :: FileSimple;
# use strict;
#use warnings;
# 说明:
# 遍历出 "/cygdrive/c/Documents and Settings/Administrator/"
# 满足 "^.*\.txt$" 添加文件
# 并运行 "ls -l $fileAllName "
# 运行日志 ./log
#
# perl this.pl
# "ls -l $fileAllName "
# "/cygdrive/c/Documents and Settings/Administrator/"
# "^.*\.txt$"
# ./log
#
# run_comm 参数1 替换说明:
# $filePath 为输入参数2,遍历根目录 "/cygdrive/c/Documents and Settings/Administrator/"
# $fileAllName 为当前遍历,满足正则条件的 全路径+文件名
# $logicFileAllName 为当前遍历,满足正则条件的 逻辑路径+文件名
# $logicFilePath 为当前遍历,逻辑路径
# $logicFileName 文件名称
# $logicFileExp 文件后缀
# (test.txt 文件
# $logicFileName 为 test
# $logicFileExp 为 txt
# )
my $run_comm = $ARGV [ 0 ];
my $file_path = $ARGV [ 1 ];
my $file_find_rex = $ARGV [ 2 ];
my $logfile = $ARGV [ 3 ];
my $log = new Log :: FileSimple(name => ' Log::FileSimple logs ' ,
file => " $logfile " ,
mask => - 1 ,
autoflush => 0 ,
);
sub fileThumb{
eval {
$_tmp_run_comm = $run_comm ;
if ( / $file_find_rex / ){
$fileNum ++ ;
$_tmp_run_comm =~ s /\ $filePath / $file_path / g;
my $_file_all_name = $File :: Find :: name;
$_tmp_run_comm =~ s /\ $fileAllName / $_file_all_name / g;
my $_logic_file_all_name = $_file_all_name ;
$_logic_file_all_name =~ s / $file_path // g;
$_logic_file_all_name =~ s /^\ s *\.// g;
$_tmp_run_comm =~ s /\ $logicFileAllName /\.\/ $_logic_file_all_name / g;
if ( $_logic_file_all_name =~/^ ( .* ) \/ ([ ^\/ ] * ) \. ([ ^\. ] * )$ / ){
my $_logic_file_path = $ 1 ;
my $_logic_file_name = $ 2 ;
my $_logic_file_exp = $ 3 ;
$_tmp_run_comm =~ s /\ $logicFilePath /\.\/ $_logic_file_path / g;
$_tmp_run_comm =~ s /\ $logicFileName / $_logic_file_name / g;
$_tmp_run_comm =~ s /\ $logicFileExp / $_logic_file_exp / g;
}
# print $_tmp_run_comm,"\n";
my @return = eval { system ( " $_tmp_run_comm 2>&1 \|echo \$\? " )};
if ( $return [( @return - 1 )] != 0 ) {
$log -> log (message => " success($fileNum):$_file_all_name " );
} else {
$log -> log (message => " error($fileNum):$_file_all_name " );
}
}
}
}
find( \& fileThumb , $file_path );
use File :: Find;
use Log :: FileSimple;
# use strict;
#use warnings;
# 说明:
# 遍历出 "/cygdrive/c/Documents and Settings/Administrator/"
# 满足 "^.*\.txt$" 添加文件
# 并运行 "ls -l $fileAllName "
# 运行日志 ./log
#
# perl this.pl
# "ls -l $fileAllName "
# "/cygdrive/c/Documents and Settings/Administrator/"
# "^.*\.txt$"
# ./log
#
# run_comm 参数1 替换说明:
# $filePath 为输入参数2,遍历根目录 "/cygdrive/c/Documents and Settings/Administrator/"
# $fileAllName 为当前遍历,满足正则条件的 全路径+文件名
# $logicFileAllName 为当前遍历,满足正则条件的 逻辑路径+文件名
# $logicFilePath 为当前遍历,逻辑路径
# $logicFileName 文件名称
# $logicFileExp 文件后缀
# (test.txt 文件
# $logicFileName 为 test
# $logicFileExp 为 txt
# )
my $run_comm = $ARGV [ 0 ];
my $file_path = $ARGV [ 1 ];
my $file_find_rex = $ARGV [ 2 ];
my $logfile = $ARGV [ 3 ];
my $log = new Log :: FileSimple(name => ' Log::FileSimple logs ' ,
file => " $logfile " ,
mask => - 1 ,
autoflush => 0 ,
);
sub fileThumb{
eval {
$_tmp_run_comm = $run_comm ;
if ( / $file_find_rex / ){
$fileNum ++ ;
$_tmp_run_comm =~ s /\ $filePath / $file_path / g;
my $_file_all_name = $File :: Find :: name;
$_tmp_run_comm =~ s /\ $fileAllName / $_file_all_name / g;
my $_logic_file_all_name = $_file_all_name ;
$_logic_file_all_name =~ s / $file_path // g;
$_logic_file_all_name =~ s /^\ s *\.// g;
$_tmp_run_comm =~ s /\ $logicFileAllName /\.\/ $_logic_file_all_name / g;
if ( $_logic_file_all_name =~/^ ( .* ) \/ ([ ^\/ ] * ) \. ([ ^\. ] * )$ / ){
my $_logic_file_path = $ 1 ;
my $_logic_file_name = $ 2 ;
my $_logic_file_exp = $ 3 ;
$_tmp_run_comm =~ s /\ $logicFilePath /\.\/ $_logic_file_path / g;
$_tmp_run_comm =~ s /\ $logicFileName / $_logic_file_name / g;
$_tmp_run_comm =~ s /\ $logicFileExp / $_logic_file_exp / g;
}
# print $_tmp_run_comm,"\n";
my @return = eval { system ( " $_tmp_run_comm 2>&1 \|echo \$\? " )};
if ( $return [( @return - 1 )] != 0 ) {
$log -> log (message => " success($fileNum):$_file_all_name " );
} else {
$log -> log (message => " error($fileNum):$_file_all_name " );
}
}
}
}
find( \& fileThumb , $file_path );
本文转自博客园刘凯毅的博客,原文链接:perl 工具小脚本,如需转载请自行联系原博主。