Calculate time stamp difference


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate time stamp difference
# 1  
Old 07-11-2011
Calculate time stamp difference

Hi All,

I am new to shell scripting.I have to write a shell script for the problem statement:

"A file is updated continously. If it is not updated for a day then an error message needs to pop up."

So the script needs to read the last modified time of that file and current system time .If the difference is more than 1 day then error message needs to be displayed.


Any help is much appreciated.Smilie

Thanks,
Bharath
# 2  
Old 07-11-2011
Run below script using

<scriptname> <filename>


Code:
filename=$1
filetimestamp=`ls -lrt $filename| awk '{print $7}'`
current_date=`date +%d`
a=`expr $current_date - $filetimestamp`
if [ $a -le 1 ] ; then
echo "File was last updated on `date`"
else
echo "File was not updated from one day"
fi


Cheers
Harish

Last edited by harish612; 07-11-2011 at 08:16 AM..
This User Gave Thanks to harish612 For This Post:
# 3  
Old 07-11-2011
Thanks. Need some more help

Harish,

Thanks a lot for your quick response.

Can you please help me further in modifying code so that 24 hours difference is calculated in accurate sense.

That is calculating 24hrs diiference taking into consideration date, month and year also into consideration.Smilie


Regards,
Bharath
# 4  
Old 07-12-2011
Bharath,
Please check below code and try to run using

<scriptname> <filename>


Code:
filename=$1
filedate=`ls -lrt $filename| awk '{print $7}'`
filetimestamp=`ls -lrt $filename | awk '{print $8}' |awk -F ":" '{print $1 $2}'`
fileyear=`ls -lrt $filename | awk '{print $8}'`
filetime=`ls -lrt $filename | awk '{print $8}'`
filemonth=`ls -lrt $filename | awk '{print $6}'`
current_month=`date +%c | awk '{print $2}'`
current_year=`date +%c | awk '{print $5}'`
current_date=`date +%d`
prev_date=`expr $current_date - 1`
date_diff=`expr $current_date - $filedate`
current_time=`date +%H%M`
time_diff=`expr $current_time - $filetimestamp`
if [[ ${#fileyear} -eq 4 ]] ; then
if [ $fileyear -ne $current_year ]  ; then
echo "File is not of year $current_year"
exit
elif [ $filemonth != $current_month ] ; then
echo "File is of year $current_year but not $current_month month"
exit
fi
elif [ $filemonth != $current_month ] ; then
echo "File is of year $current_year but not $current_month month"
exit
fi
 
if [ $filedate -eq $prev_date ] || [ $filedate -eq $current_date ]; then
if [ $filedate -eq $current_date ]; then
echo "File was last updated on $filedate $filemonth $current_year at $filetime"
elif [ $filedate -eq $prev_date ] ; then
if [ $time_diff -le 0 ] ; then
echo "File was last updated on $filedate $filemonth $current_year at $filetime"
else
echo "File was updated on $filedate $filemonth $fileyear but before 24 hours from now"
fi
fi
else
echo "File was updated before $prev_date $current_month $current_year"
fi


Cheers
Harish

Last edited by harish612; 07-12-2011 at 07:08 AM..
# 5  
Old 07-12-2011
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Difference between time stamp

Hi All i have a file data like below format A, B 2016-04-14 16:30:00,2016-04-14 16:31:17 2016-04-14 16:40:00,2016-04-14 16:41:10 2016-04-14 16:50:00,2016-04-14 16:50:41 2016-04-14 17:00:00,2016-04-14 17:00:35 2016-04-14 17:10:00,2016-04-14 17:11:48 2016-04-14 17:20:00,2016-04-14 17:20:37 i... (2 Replies)
Discussion started by: Tarak_nath
2 Replies

2. Shell Programming and Scripting

Calculate time difference

I have time in a file in HH:MM:SS format as it contents(its not the file creation time). i need this to be converted to epoch time or time since 1970. The time is written into that file by a script, which i cannot modify. Im using AIX machine $ cat abc.txt 10:29:34 (2 Replies)
Discussion started by: gpk_newbie
2 Replies

3. Shell Programming and Scripting

To check time stamp in log file and calculate.

Hi Friends, I have the following logfile. i want to make a script for calculate time by time2 - time1 1600266278|random|1|2014-09-19 02:08:56.024|2014-09-19 02:08:59.398|A|B|ROOM|Num0208559970111101788|1|dog|dos 1600266200|random|4|2014-09-19 02:08:06.572|2014-09-19... (2 Replies)
Discussion started by: ooilinlove
2 Replies

4. Shell Programming and Scripting

Time stamp Difference

I have a log file which wrote time stamp like this 2013-02-11 00:46:40.389037 2013-02-12 11:46:40.197045 can any one help me to get the time stamp difference of these two line in seconds. (4 Replies)
Discussion started by: netdbaind
4 Replies

5. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

6. Shell Programming and Scripting

Calculate the Time stamp difference

hi, I have a log file which gives time stamps hh:mm:ss.sssss format in which hh- hours , mm -minutes ss.sssss - seconds.microseconds I need to calculate the time diff between sent time stamp and received time stamp .... could any one please help me.. i am tryin to write a script but... (2 Replies)
Discussion started by: firestar
2 Replies

7. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

8. Shell Programming and Scripting

How to calculate the time difference.

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -1 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" | awk '{print $3,$4,$5,$6}' >> ${RESULTFILE} The output comes as below: 2008-09-30 06.00.01... (2 Replies)
Discussion started by: suri.tyson
2 Replies

9. Shell Programming and Scripting

How to calculate the time difference...

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -2 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" |head -1 | awk '{print echo "PREVIOUS:-- Start Date&Time: " $3,$4,echo "|| End Date&Time:... (1 Reply)
Discussion started by: suri.tyson
1 Replies

10. Shell Programming and Scripting

How to calculate this time difference

Hi, Please help me in calculating the time difference between below mentioned timestamps. a=07/17/2007 02:20:00 AM MST b=07/17/2007 02:07:46 AM MST Thanks (2 Replies)
Discussion started by: Prat007
2 Replies
Login or Register to Ask a Question