![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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.. |
|
||||
|
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... |
|
|||||
|
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. |
|
||||
|
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|