Time comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Time comparison
# 1  
Old 01-29-2009
Time comparison

hi friends,
I am new to shell scripting and i am using KSH shell .I would like to automate our daily routine manual tasks .first i ll explain the situation .I will list out the contents of directory named "log" using "ls " and verify whether all the listed files time differences is not more than 30 minutes.if not, i will send the mail. Now , i need to automate this . i think i can take the time[for eg: 00:00 ,00:30,01:00,01:30....23:00] alone by using "awk" command or "date" function how can i compare the time values because i am having approximately[not exactly] 48 files[24/0.5] per day i need to check the time difference between above 48 files[for every 30 minutes once one file will be created ] .based on that i will do some other tasks...

My time stamp is "Jan 28 18:48"

Eg: ls -l log
file 1 00:30 file 2 - file 1 is 30 minutes
file 2 01:00
file 3 01:30 file 3 - file 2 is 30 minutes

can anyone help me ? Thanks in advance .....

Last edited by rdhaprakasam; 01-29-2009 at 07:33 PM..
# 2  
Old 01-30-2009
Here is one approach which works across midnight time change-
Code:
#!/bin/ksh

filetime()  # file time in epoch seconds
{
    perl  -e '          
          print (stat $ARGV[0])[9];
         ' $1
}

istooold()  # return 0 if too old, 1 if okay
{
	now=$(date +%s)       # epoch time in seconds
	ftime=$(filetime "$1")   # file age in seconds
	now=$( now - 1800 )   # 30 minutes ago 
	if [[ $ftime -ge $now ]] ; then
		print 1
	else
	    print 0
	fi	
}

for filename in  $(ls log)
do
	ok=$( istooold $filename)
	if [[ ok -eq 1 ]] ; then
	   echo "$filename is okay"
	else
	   echo "$filename is too old"
	fi   
done

# 3  
Old 01-30-2009
Tools

thanks guru .. i will try and let you know what is happening
# 4  
Old 02-01-2009
Quote:
Originally Posted by jim mcnamara
Here is one approach which works across midnight time change-
[code]
filetime() # file time in epoch seconds
{
perl -e '
print (stat $ARGV[0])[9];
' $1

That will break if any filenames contain spaces.
Quote:
Code:
}
for filename in  $(ls log)


That will break if any filenames contain spaces. But it won't work anyway, because the filenames will not contain the directory.

Code:
for filename in log/*

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date and Time comparison using shell script

Hi, I'm having two fields in the file F1|F2 20111220|102000 F1 ->YYYYMMDD F2 ->HHMMSS Now, I need to compare this with current date & time and need to return the difference value in hours. Already, I checked with datecalc from the forum. So, need hints from Shell Gurus. Thanks (10 Replies)
Discussion started by: buzzusa
10 Replies

2. Shell Programming and Scripting

File modification time comparison

Hi All, I have two files (given below) each exists under different paths. I want to compare the modification time stamp of file1.txt is lessthan the modification time of file2.txt. month1=`ls -l file1.txt | awk '{ print $6}'` date1=`ls -file1.txt | awk '{ print $7}'` time1=`ls... (1 Reply)
Discussion started by: Arunprasad
1 Replies

3. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

4. Shell Programming and Scripting

System time comparison to fixed defined time

I have a requirement of checking the current system time and performing certain actions in a shell script. example: if the current system time is greater than 1400 hrs, then perform step 1,2,3 if the current system time is greater than 1000 hrs, then perform step 1,2 if the current system time... (2 Replies)
Discussion started by: zainravi
2 Replies

5. Shell Programming and Scripting

Date comparison in file to system time

I have a CSV (comma separated vaule) file whose entries resemble Area,\\ntsvsp02\vmcs\download\files\Areas.dat,1,20090303,0,Import Complete,2009-03-02 04:23:00 Product,\\ntsvsp02\vmcs\download\files\items.dat,1,20090303,0,Import Complete,2009-03-02 04:23:00... (3 Replies)
Discussion started by: zainravi
3 Replies

6. Shell Programming and Scripting

Files manipulation with time comparison

Hi guys - I am new to Unix and learning some basics. I have to create a report of files that are in a specific directory and I have to list filenames with specific titles. This report will be created everyday early in the morning, say at 05:00 AM. (see output file format below) The 2 categories... (2 Replies)
Discussion started by: sksahu
2 Replies

7. Shell Programming and Scripting

Help with time comparison shell script for HP-UX

I am using Korne Shell in HP-Ux. Can someone give me and idea on how I can write a shellscript on how to do this please:- On our HP-UX server, a batch file is run every evening at about 6:30pm. The first step of this batch file will touch an empty "flag" file to indicate that the batch has... (6 Replies)
Discussion started by: gummysweets
6 Replies

8. Shell Programming and Scripting

Newbee Needs Help - Time comparison

I am very new to Unix so please bear with me! I am trying to write a script that will compare file creation time with the current time so I can be notified if a file is more than an hour old. Can anybody point me in the right direction on this??? Thank you in advance for any help!!! It is GREATLY... (4 Replies)
Discussion started by: danedder
4 Replies

9. UNIX for Advanced & Expert Users

File Time Comparison Question

I want a cron job to fire off every 5 minutes or so to verify that a file in a directory is not more than 15 minutes old (from the current time. If the newest file is more than 15 minutes old, I would fire off an email.. The email part is easy, but I'm having trouble figuring the logic... (2 Replies)
Discussion started by: pc9456
2 Replies
Login or Register to Ask a Question