Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

In Bash script, I would like to compare two dates and if one is greater than the other, carry out a task.

Two dates that I'm comparing are:

svn repo's last change date, I get the date info like so:

svn info svn://server.com/reponame -r 'HEAD' | grep 'Last Changed Date'

It gives me something like this:

Last Changed Date: 2011-06-06 22:26:50 -0400 (Mon, 06 Jun 2011)

Then I'm finding the date information of the most current backup file in the directory, like so:

ls -lt --time-style="+%Y-%m-%d %H:%M:%S" | head -n 2 | tail -n 1

(I wonder if there is a better way to do it with out both head and tail above)

Which gives me something like this:

-rw-r--r-- 1 user1 user1 14 2011-06-09 07:52:50 svn.dump

What I would like to do next, is to retrieve date from first output and compare it with second output date and if one is greater than the other one, I will do an svn dump. what would be the most appropriate way to do this?

Thanks.

share|improve this question
    
No time for pondering the date comparison stuff (I usually use Perl and its Date::Manip module), but a quick note about ls replacement: how about stat -c "%y" svn.dump, or if you truly want to mimic ls output, stat -c "%A %U %G %h %y %n" svn.dump –  Janne Pikkarainen Jun 9 '11 at 12:13
    
Stat seems like a function I would want to use, stat -c "%X" svn dump gives me seconds. Now if I could get the last modified date from svn in seconds... –  user80666 Jun 9 '11 at 12:25

3 Answers 3

/bin/date can convert time to formats, including seconds from epoch.

DATE_1="`svn info svn://server.com/reponame -r 'HEAD' | grep 'Last Changed Date' | grep -E -o \"[0-9]{4}-[0-9]{2}-[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2}\"`"

DATE_2="`ls -lt --time-style="+%Y-%m-%d %H:%M:%S" | head -n 2 | tail -n 1 | grep -E -o \"[0-9]{4}-[0-9]{2}-[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2}\"`"

if [ "`date --date \"$DATE_1\" +%s`" -gt "`date --date \"$DATE_2\" +%s`" ]; then
    echo "Greater"
else
    echo "Less or equal"
fi
share|improve this answer
    
+1 for not forgetting how to convert the svn date to a comparable date like I did :) –  Server Horror Jun 9 '11 at 12:44
## Specify the filename to dump here
_filename = svn.dump

## Grab the mtime of the file and print out in "seconds since Epoch format"
_fileepoch=$(find ${_filename} -printf "%Ts")

## Grab the last changed date of the file and store it in _svndate
_svndate=$(svn info svn://server.com/reponame -r 'HEAD' \
              | grep 'Last Changed Date' \
              | awk '{print $4, $5, $6}')

## Use date --date with the "%s" format specifier to print seconds since Epoch
## for that date
_svnepoch=$(date --date "${_svndate}" "+%s")

You should be able to compare ${_svnepoch} and ${_fileepoch} now

share|improve this answer

I'd simply compare on number of seconds since the epoch ls -lt --time-style="+%s". That gives you a nice number you can compare on.

From man bash

   arg1 OP arg2
          OP  is  one  of -eq, -ne, -lt, -le, -gt, or -ge.
          These arithmetic binary operators return true if
          arg1 is equal to, not equal to, less than, less
          than or equal to, greater than, or greater than or
          equal to arg2, respectively.  Arg1 and arg2 may be
          positive or negative integers.

Easy enough to do.

F1=$(ls -l --time-style='+%s' file1|awk '{ print $6 }')
F2=$(ls -l --time-style='+%s' file2|awk '{ print $6 }')
if [ $F1 -gt $F2 ]; then
    echo yay
    echo F1: $F1
else
    echo nay
    echo F2: $F2
fi

As others have posted, a scripting language that actually gives meaning to a date probably is better suited for these kind of tasks

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.