The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare dates... i_priyank Shell Programming and Scripting 3 09-21-2007 01:50 AM
compare dates ragha81 Shell Programming and Scripting 2 11-01-2006 06:17 PM
How to compare dates in C/C++ naan High Level Programming 1 08-28-2006 11:03 AM
How to compare two dates bankpro High Level Programming 5 01-24-2006 04:07 AM
compare two dates wchen Shell Programming and Scripting 6 10-24-2002 05:39 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-22-2008
suri.tyson suri.tyson is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 20
How to compare the dates..

Hi all,

I've written a script which gives the below information...

End Date&Time: 2008-10-21 10.54.37

Now i want to calculate this time with the current time.. and if its more than 48 hours past with the current time it should echo "48 Hours back"

Please help me..

thanks in advance..

  #2 (permalink)  
Old 10-22-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Maybe a different way to approach this task

I can determine 'seconds since Epoch' on a file modification

Code:
> touch file286
> stat -c%Y file286
1224687447

Touch'ed another file a little later - note greater number

Code:
> touch file287
> stat -c%Y file287
1224687482

Now I can see that 35 seconds actually elpased

Code:
> echo `stat -c%Y file287` - `stat -c%Y file286` | bc
35

So, by extension, you could touch a temp file (like my file287) and then compare the number of seconds difference. Just do the math to figure out how many seconds in your desired elapsed time period.

Last edited by joeyg; 10-22-2008 at 12:03 PM.. Reason: corrected spelling
  #3 (permalink)  
Old 10-22-2008
suri.tyson suri.tyson is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 20
The problem here is..?

When the dates are not matching... it could be a problem...


Guess this could not work...


Hope we have to find the current date and matching with the log file date.. and subtract and if less than blah blah then + or - 86400...

dont know how to write the same...

Please help...
  #4 (permalink)  
Old 10-22-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Since you said you had a script which gives ending date/time... figured you could

Code:
>touch /home/user/script_123.done

as the final command

Then your new script could

Code:
>touch /home/user/script_123.restart

And then do the math like I previously showed to see if the elapsed seconds had in fact elapsed.


Code:
>time_elap=$(echo `stat -c%Y script_123.restart` - `stat -c%Y script_123.done` | bc)

Now, just do an if-then for your time calculation and possible next steps.
  #5 (permalink)  
Old 10-22-2008
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,807
joeyg's answer is MUCH easier than coding this:

Code:
#!/bin/ksh

# two parms
# $1 = number of hours
# $2 = date in YYYY mm dd hh mi ss format
# prints  1 if date is older than  now minus <number of hours>
# prints 0 when not older
isolder()
{
	ago="$1"
	echo "$2" | sed 's/\./ /g' | sed 's/-/ /g' | read dt
	perl -e  ' 
	        use Time::Local;
	        $then = time - ( $ARGV[0] * 3600);
	        $testtime =
	           timelocal( $ARGV[6], $ARGV[5], $ARGV[4],
	                      $ARGV[3], $ARGV[2]-1, $ARGV[1]-1900);
	        $result = 0;
	        if ( $testtime < $then )
	        {
	            $result = 1;
	        }
            print "$result\n";
	        '  $ago  $dt
}

dt="2008-10-21 10.54.37"
# sample usage:
result=$(isolder 48 "$dt" )
if [[ $result -eq 1 ]] ; then
  echo "$dt is older than 48 hours"
else
  echo "$dt is not older than 48 hours"  
fi  
dt="2008-10-19 10.54.37"
result=$(isolder 48 "$dt" )
if [[ $result -eq 1 ]] ; then
  echo "$dt is older than 48 hours"
else
  echo "$dt is not older than 48 hours"  
fi

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0