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 3169 KevinADC 4,059
Recognized Expert Specialist
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.
miller 1,089
Recognized Expert Top Contributor
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
KevinADC 4,059
Recognized Expert Specialist
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.
miller 1,089
Recognized Expert Top Contributor
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
numberwhun 3,509
Recognized Expert Moderator Specialist
And allow me to add "use warnings;" as well. :-)
Jeff
miller 1,089
Recognized Expert Top Contributor
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
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. So I've put together an
Access database with three tables - my media library, a list of friends
and email addresses, and a borrow table which uses foreign keys from the
other two tables to see who has borrowed what. Borrow Table also has two...
|
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 somehow adding
filename and date created in an array, sorting by date and then
printing onto an html page with a link to that file. How would I do
this though? Is this the simplest method or is there a better way of
doin this?
|
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 compare them as if they were
datatables... (ie a simple diff type tool won't work)...
More specifically, the process I envision so far is:
1) Read in two files -- if Excel select appropriate sheet (would also
require users to structure their...
|
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. DateTime.Now and something like (AddDays(DateTime.Now,-5) are used
for the inner and outer ranges, its how to express the "between" that has me.
/* what Id like to do, in pseudo code */
dateToTest=DateTime.Parse("mm/dd/yy");
|
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 can I fix this?
Tks in advance...
This is what I have:
'Set today's date
| |
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 beyond 90 days from today's date, the Expiration Date should be
today's date plus 90 days. If the entered date is less than 90 days
|
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 a software buffer of size 3MB. This is needed so as to see which function to use to read from the file. As the files used range from very large (>30GB) to very small (<3MB), I have enabled large file support and I obtain the file size by using the...
|
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 22:02:51? (24 hour day is fine)
How about the date after 02/28/04 is 02/29/04, or the date after 09/30/08 is
10/01/08?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |