Hi the purpose of the script I am trying to do is to access a dir and run the ls -l command which will show the files and the modified date, no w the script is to check two files in that dir , if the file has the modified month and day matching today print message log is fine else the log is not updated and thats it.
Now the script below I managed to parse to get both month and day for the files and the perl day and month
now the script shows successful when showing today but i changed the perl time to yesterday's date by hard coding a value and it still shows the log updated message when it suppose to show log not updated, so it is a problem i am guess ign with my else but i think i did it correct but i cannot seem to find where i am going wrong, could somebody help me out please. -
#!/usr/bin/perl
-
#Purpose: Monitor script to monitor if logs is todays date
-
-
#Uncomment to use live perl time
-
# @datepcs = split(/\s+/, scalar localtime);
-
# printf ("%s %s %s\n", @datepcs[1,2]);
-
-
#comment to remove hard coded value
-
$dateTime ="Jul 26";
-
print $dateTime;
-
-
chdir("/home/tibco/tibco/ACS_MQ_ADAPTER/logs");
-
sleep 2;
-
$test = "ls -l |";
-
open (INFO, "$test") || die " Unable to run command $!";
-
@rows = <INFO>;
-
-
open (TEST, ">/home/tibco/tibco/acs_monitor.txt") || die " Error could not open log $!";
-
foreach (@rows) {
-
-
if ($_ =~ m/MQJMSListener.log/) {
-
###### Writing to file
-
print TEST "$_";
-
$_ = split;
-
$dates = "$_[5]";
-
$dates2 ="$_[6]";
-
print $dates . " $dates2";
-
#if ($dates . " $dates2" == "$dateTime") {
-
if ("$dateTime" == $dates . " $dates2") {
-
print "\n This $_[8] is fine \n";
-
print TEST '<span class="green">This log matches todays date</span><br>';
-
} else {
-
print "\n Dates do not match for $_[8] \n";
-
print TEST '<br><br><span class="red">This log does not match todays date</span><br>';
-
}
-
}
-
-
if ($_ =~ m/EMSListener.log/) {
-
###### Writing to file
-
print TEST "$_";
-
$_ = split;
-
$dates = "$_[5]";
-
$dates2 ="$_[6]";
-
-
if ($dates . " $dates2" == "$dateTime") {
-
print "\n This $_[8] is fine \n";
-
print TEST '<span class="green">This log matches todays date</span><br>';
-
} else {
-
print "\n Dates do not match for $_[8] \n";
-
print TEST '<span class="red">This log does not match todays date</span><br>';
-
}
-
}
-
}
-
close(TEST);
-
close(INFO);
-
#END
-
12 3135
Maybe you should be checking string equality instead of numeric equality:
if ($dateTime eq "$dates$dates2") {
never quote scalars (unless absolutely necessary) like you did in this "if" condition:
if ("$dateTime" == $dates . " $dates2") {
only quote variables like I did above when you are constructing a new string. Quoting can hide errors that make debugging a script near impossible, even if you are using "my" and "warnings". Plus it slows down your script because each double-quoted scalar makes a new copy of the scalar that never gets used if you do not assign it to a new scalar or use it in an operation like I did above to compare the two strings. It is a bad habit you should try and rid yourself of earlier instead of later.
Instead of parsing an external process, why not just stick with a pure perl solution? perldoc stat perldoc -X
The following script prints any file that is been modified today in the specified directory. This could easily be modified to serve your purposes. -
use POSIX qw(strftime);
-
-
use strict;
-
-
my $today = strftime "%Y%m%d", localtime;
-
-
my $dir = "/home/tibco/tibco/ACS_MQ_ADAPTER/logs";
-
-
opendir(DIR, $dir) or die "Can't open $dir: $!";
-
while (my $file = readdir(DIR)) {
-
next if $file =~ /^\.+$/;
-
-
my $path = "$dir/$file";
-
my $modified = (stat($path))[9];
-
my $date = strftime "%Y%m%d", localtime($modified);
-
-
print "$file modified today\n" if $date eq $today;
-
}
-
closedir(DIR);
-
- Miller
this also looks wrong:
$_ = split;
assigning the entire return value of split to a scalar should return the number of splits. You need to assign the return value to a list or array:
@data = split;
and are you sure you want to just use "split" with no arguments?
Thanks guys the scripts you gave are really good in a pure perl solution i am still a novice thats why i went this way.
hey guys i made a big mistake, the if statements were fine,
I forgot to declare a variable for dateTime for the perl time section
I only devlared it for hard coding the values. Well it works well withthe hard coded values but how do I put this in variable, i think that is my issue now
this is what i did
I am guess to manipulate the array? - How would i put this in a variable?
-
-
@datepcs = split(/\s+/, scalar localtime);
-
printf("%s %s", @datepcs[1,2]);
oh so i guess it would of bee @row = split;
yes i read split; parses by spaces by default.
this also looks wrong:
$_ = split;
assigning the entire return value of split to a scalar should return the number of splits. You need to assign the return value to a list or array:
@data = split;
and are you sure you want to just use "split" with no arguments?
Hey guys I got everything working. Thanks so much for your help.
I guess i learned to do some more troubleshooting and Miller thanks for the script, it is very interesting in my learning of perl. I learned alot from all of you guys on the forum and thanks for helping me once again I appreciate it.
Your welcome. Glad you were able to get your script to work.
Btw, the best way you could improve this script or any perl script would be to add a "use strict;" statement to the top.
- Miller
And allow me to add "use warnings;" as well. :-)
Jeff
I've started using warnings more often. However, I typically consider most of things that it "warns" about as rather superfluous.
The biggest problem that I have with it though is that it provides runtime warnings instead of compile time like "use strict;". This means that I leave "use warnings;" out of any production level code as I do not want to cause the end-user needless worry for something that doesn't matter.
"use strict:" however should be included in any piece of code.
- Miller
You can use the -M file operator which returns the date of a file in days since the program started. So if you wanted to see if files are matcing todays date you would see if the number returned by -M is less than one. - chdir('c:\\');
-
@files = <*>;
-
for (@files){
-
if ((-M) < 1) {
-
print $_,"\n";
-
}
-
}
I considered using that in the solution that I provided, which is why I included a link to the -X documentation. However, I figured a better introduction to a pure perl solution would closely match what he was already attempting to do. Namely match the current date, not just within 24 hours. Although I suspect that the range would actually be more favorably functionality.
- Miller
Certainly. My post is a complimentary extension of what you previously posted, at least I hope it is. ;)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Daniel Fisher |
last post by:
Hi All!
I'm fairly new to PhP and basicly trying to learn right now. Now I have
a problem - I have a fairly large collection of movies which people keep
borrowing from me. And then not returning....
|
by: bissatch |
last post by:
Hi,
I am about to write an application that will display all pdf files in a
folder and display them by file name in order of date.
Is there an easy way of doing this? I was thinking about...
|
by: Tim Brooks |
last post by:
All,
I'm hoping one of you Xml or Data gurus can offer an opinion. I'm working
on an app to basically compare two semi-structured data files (e.g. Excel /
CSV) to one another. But I need to...
|
by: MarkAurit |
last post by:
Im having difficulty coming up with a good algorithm to express the following
comparison:
"if <a given date> falls between the (current date - 5 days) and the
(current date)"
Obviously....
|
by: JFB |
last post by:
Hi All,
I'm trying to find if a file name that includes the date is before todays to
do some procedures.
In my IF statement is working is just a few dates before but not is a year
before.
How...
|
by: blini |
last post by:
Helo.... How I can convert string "26/03/2006 15:51" for a date? I need
to convert and to compare if "09/06/2006 14:20" is lesser or equal that
the current date. Everything in Javascript.
|
by: Brett_A |
last post by:
I have the following code:
If ad_expiration_date (date() + 90) then
ad_expiration_date = (date() + 90)
else
end if
What I want to happen is if the ad_expiration_date entered by the user
is...
|
by: Lars B |
last post by:
Hey guys,
I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers.
In my program I need to compare the size of a given file against...
|
by: W. eWatson |
last post by:
Are there some date and time comparison functions that would compare, say,
Is 10/05/05 later than 09/22/02? (or 02/09/22 format, yy/mm/dd)
Is 02/11/07 the same as 02/11/07?
Is 14:05:18 after...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |