linux - perl while loop -
in code parse file (containing output ls -lrt
) log file's modification date. move log files new folder modification dates added filenames, , making tar of files.
the problem getting in while
loop. because it's reading data files while loop keeps on running 15 times. understand there issue in code can't figure out.
inside while loop splitting ls -lrt
records find log file modified date. $file
output of ls
command storing in text file /scripts/yagya.txt
in order modification date. while
loop executing 15 times since there 15 log files in folder match pattern.
#!/usr/bin/perl use file::find; use strict; @field; $filenew; $date; $file = `ls -lrt /scripts/*log*`; $directory="/scripts/*.log"; $current = localtime; $current_time = $current; $current_time = s/\s+//g; $freetime = $current_time; $daytime = substr($current_time,0,8); $seconddir = "/$freetime/"; system ("mkdir $seconddir"); open (myfile,">/scripts/yagya.txt"); print myfile "$file"; close (myfile); $data = "/scripts/yagya.txt"; $datas = "/scripts/"; %options = ( wanted => \&wanted, untaint => 1 ); find (\%options, $datas); sub wanted { if (/[._]log\d*$/){ $files; @fields; $fields; chomp; $files=$_; open (myfile,$data); while(<myfile>){ chop; s/#.*//; next unless /\s/; @fields = (split)[5,6,7]; $fields = join('',@fields), "\n"; } close (myfile); system ("mv $files $seconddir$fields$files"); } } system ("tar cvf /$daytime/$daytime.tar.gz /$daytime/*log*"); system ("rm $seconddir*log*"); system ("rm $data");
your code difficult read. looks have written program single big chunk before started test it. way of working common wrong. should start implementing small part of program , testing before add little more functionality, test again, , on. way won't overwhelmed fixing many problems @ once in large untested program.
it lot if added use warnings
use strict
@ top of program. helps catch simple errors may overlook.
also, aware file::find
call wanted
callback subroutine every time encounters file? doesn't pass files @ once.
the problem seems reading way through yagya.txt
file when should stopping when find record matches current file file::find
has found. need check whether current record in ls
output ends name of current file. if write loop this
while (<myfile>) { if (/\q$files\e$/) { @fields = (split)[5,6,7]; $fields = join('',@fields); last; } }
then $fields
end modification date of current file, want.
but thousand times easier if used perl read file modification date you.
instead of writing ls
listing file , reading back, should this
use file::stat; $mtime = localtime(stat($files)->mtime);
which give string wed jun 13 11:25:23 2012
. date ls
output includes month name, day of month, , time of day, jun 8 12:37
. isn't specific , perhaps should @ least include year, generate same string $mtime
can write
my $fields = join '', (split ' ', $mtime)[1,2,3];
there lot more program, hope gets going now.
another couple of things have noticed:
the line
$current_time = s/\s+//g
should$current_time =~ s/\s+//g
remove spaces current time stringa value
sun jun 3 11:50:54 2012
reducedsunjun311:53:552012
, ,$daytime
take valuesunjun31
incorrect
Comments
Post a Comment